home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / hwp / hwp.c next >
C/C++ Source or Header  |  1995-06-20  |  77KB  |  4,051 lines

  1. /* Harry's Word Processor for Coherent 4.[02] */
  2.  
  3. /* Copyright 1993, Harry C. Pulley, IV.  Permission granted to copy hwp or
  4.    modify it in any form so long as my name and copyright remains in place.
  5.    I am not responsible for any damages caused by the program in any way, 
  6.    shape or form. */
  7.  
  8. /* Header files */
  9.  
  10. #include <curses.h>
  11. #include <stdlib.h>
  12. #ifdef OLD_ACCESS
  13. #include <access.h>
  14. #else
  15. #include <unistd.h>
  16. #endif
  17. #include <string.h>
  18. #include <fcntl.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <ctype.h>
  23.  
  24. /* Defines */
  25.  
  26. #if !defined (MGR) && !defined (X11)
  27. #undef A_UNDERLINE
  28. #define A_UNDERLINE (A_REVERSE|A_BOLD)
  29. #endif
  30.  
  31. #define CASE 1
  32. #define NOCASE 2
  33.  
  34. #ifndef SPOOLER
  35. #define SPOOLER "lpr -B -c"
  36. #endif
  37.  
  38. #define LINE_LENGTH 80
  39.  
  40. #define LINES_PER_PAGE 21
  41.  
  42. #define NORMAL 0x0000
  43.  
  44. #define START_BOLD 0x0100
  45. #define END_BOLD 0x0200
  46.  
  47. #define START_ITALIC 0x0400
  48. #define END_ITALIC 0x0800
  49.  
  50. #define START_UNDERLINE 0x1000
  51. #define END_UNDERLINE 0x2000
  52.  
  53. #define START_FOOTNOTE 0x4000
  54. #define END_FOOTNOTE 0x8000
  55.  
  56. #define OVERSTRIKE 1
  57. #define INSERT 2
  58.  
  59. #define mybox(win,lines,cols)\
  60. box(win,(ACS_VLINE|A_BOLD),(ACS_HLINE|A_BOLD));\
  61. wattron(win,A_BOLD);\
  62. mvwaddch(win,0,0,ACS_ULCORNER);\
  63. mvwaddch(win,0,cols-1,ACS_URCORNER);\
  64. mvwaddch(win,lines-1,0,ACS_LLCORNER);\
  65. mvwaddch(win,lines-1,cols-1,ACS_LRCORNER);\
  66. wattroff(win,A_BOLD);\
  67. wrefresh(win);
  68.  
  69. /* Globals */
  70.  
  71. char **word_list=NULL;
  72.  
  73. int num_words=0;
  74.  
  75. short *file_buffer=NULL;
  76.  
  77. WINDOW *mystdscr,*textscr;
  78.  
  79. char filename[1024]="untitled.hwp",inputline[LINE_LENGTH+2]="";
  80.  
  81. int numlines=0,page_length=66,tab_size=8,indent=16,
  82.     left_margin=8,right_margin=72,top_blanks=5,
  83.     bottom_blanks=5,starting_page_no=1,version=2,
  84.     current_page=1,current_y=0,current_x=16,current_attr=NORMAL,
  85.     insertmode=INSERT,line_spacing=1,changed=0;
  86.  
  87. char header[LINE_LENGTH+1]="%",footer[LINE_LENGTH+1]="";
  88.  
  89. /* Forward function declarations */
  90.  
  91. void init_curses();
  92.  
  93. void end_curses();
  94.  
  95. int do_load_file();
  96.  
  97. void get_user_input();
  98.  
  99. void display_screen();
  100.  
  101. void my_exit();
  102.  
  103. void write_file();
  104.  
  105. int get_inputline();
  106.  
  107. void error_message();
  108.  
  109. int file_menu();
  110.  
  111. void do_write_file();
  112.  
  113. void load_file();
  114.  
  115. int confirm_quit();
  116.  
  117. int options_menu();
  118.  
  119. int do_menubar();
  120.  
  121. void justify();
  122.  
  123. void insert();
  124.  
  125. void print_file();
  126.  
  127. int edit_menu();
  128.  
  129. void write_ascii();
  130.  
  131. int locate_menu();
  132.  
  133. int do_search();
  134.  
  135. int spell_menu();
  136.  
  137. void get_spell_list();
  138.  
  139. void do_help();
  140.  
  141. main(argc,argv)
  142. int argc;
  143. char **argv;
  144. {
  145.     int retval;
  146.  
  147.     /* For now, only allow a single file can be edited; if none is given 
  148.        then call it 'untitled.hwp' */
  149.  
  150.     if (argc==2)
  151.         strcpy(filename,argv[1]);
  152.     else if (argc>2)
  153.     {
  154.         fprintf(stderr,"Usage: %s <filename>\n",argv[0]);
  155.  
  156.         exit(1);
  157.     }
  158.  
  159.     retval=do_load_file(filename);
  160.  
  161.     if (retval==0)
  162.     {
  163.         fprintf(stderr,"Unable to load %s\n",filename);
  164.  
  165.         exit(1);
  166.     }
  167.     else if (retval==-1)
  168.     {
  169.         fprintf(stderr,"Incorrect version of hwp in file %s\n",filename);
  170.  
  171.         exit(2);
  172.     }
  173.  
  174. #ifdef MGR
  175.     puts("\0334h");
  176. #endif
  177.  
  178.     init_curses();
  179.  
  180.     textscr=newwin(21,80,1,0);
  181.  
  182.     keypad(textscr,TRUE);
  183.  
  184.     get_user_input();
  185.  
  186.     end_curses();
  187.  
  188. #ifdef MGR
  189.     puts("\033h");
  190. #endif
  191. }
  192.  
  193. void init_curses()
  194. {
  195.     /* Initialize curses */
  196.  
  197.     mystdscr=initscr();
  198.  
  199.     if (mystdscr==NULL)
  200.     {
  201.         fputs("Error in initscr.\n",stderr);
  202.  
  203.         exit(1);
  204.     }
  205.  
  206.     noecho();
  207.  
  208.     raw();
  209.  
  210.     nonl();
  211.  
  212.     keypad(mystdscr,TRUE);
  213.  
  214.     refresh();
  215. }
  216.  
  217. void end_curses()
  218. {
  219.     /* reset terminal after curses */
  220.  
  221.     nocbreak();
  222.  
  223.     echo();
  224.  
  225.     nl();
  226.  
  227.     erase();
  228.  
  229.     refresh();
  230.  
  231.     endwin();
  232. }
  233.  
  234. int do_load_file(input_filename)
  235. char *input_filename;
  236. {
  237.     int current_file_fd;
  238.  
  239.     FILE *current_file;
  240.  
  241.     if (file_buffer!=NULL)
  242.     {
  243.         free(file_buffer);
  244.  
  245.         file_buffer=NULL;
  246.     }
  247.  
  248. #ifdef OLD_ACCESS
  249.     if (!access(input_filename,AEXISTS))
  250. #else
  251.     if (!access(input_filename,F_OK))
  252. #endif
  253.     {
  254. #ifdef OLD_ACCESS
  255.         if (!access(input_filename,AREAD))
  256. #else
  257.         if (!access(input_filename,R_OK))
  258. #endif
  259.         {
  260.             if (strstr(input_filename,".hwp"))
  261.             {
  262.                 int setup[25];
  263.  
  264.                 current_file_fd=open(input_filename,O_RDONLY);
  265.  
  266.                 read(current_file_fd,setup,25*sizeof(int));
  267.  
  268.                 numlines=setup[1];
  269.                 page_length=setup[2];
  270.                 tab_size=setup[3];
  271.                 indent=setup[4];
  272.                 left_margin=setup[5];
  273.                 right_margin=setup[6];
  274.                 top_blanks=setup[7];
  275.                 bottom_blanks=setup[8];
  276.                 starting_page_no=setup[9];
  277.                 line_spacing=setup[10];
  278.                 current_page=setup[11];
  279.                 current_y=setup[12];
  280.                 current_x=left_margin;
  281.  
  282.                 /* setup[0] is the version number - 2 means
  283.                    that the header and footer are stored
  284.                    with the file - version 1 did not store
  285.                    them */
  286.  
  287.                 if (setup[0]>2||setup[0]<1)
  288.                     return -1;
  289.                 else if (setup[0]==2)
  290.                 {
  291.                     read(current_file_fd,header,LINE_LENGTH+1);
  292.                     read(current_file_fd,footer,LINE_LENGTH+1);
  293.                 }
  294.                 else
  295.                 {
  296.                     strcpy(header,"%");
  297.                     strcpy(footer,"");
  298.                 }
  299.  
  300.                 file_buffer=(short *) malloc(numlines*LINE_LENGTH*sizeof(short));
  301.  
  302.                 if (file_buffer==NULL)
  303.                     return 0;
  304.  
  305.                 read(current_file_fd,file_buffer,numlines*LINE_LENGTH*sizeof(short));
  306.  
  307.                 close(current_file_fd);
  308.             }
  309.             else
  310.             {
  311.                 int line;
  312.  
  313.                 char tmpfilename[65],command[128];
  314.  
  315.                 numlines=0;
  316.  
  317.                 line_spacing=0;
  318.  
  319.                 left_margin=0;
  320.  
  321.                 indent=8;
  322.  
  323.                 right_margin=79;
  324.  
  325.                 top_blanks=bottom_blanks=0;
  326.  
  327.                 current_x=0;
  328.  
  329.                 current_y=0;
  330.  
  331.                 strcpy(header,"%");
  332.                 strcpy(footer,"");
  333.  
  334.                 sprintf(tmpfilename,"/tmp/hwp.%d",getpid());
  335.  
  336.                 sprintf(command,"detab < %s > %s",input_filename,tmpfilename);
  337.  
  338.                 system(command);
  339.  
  340.                 current_file=fopen(tmpfilename,"r");
  341.  
  342.                 while (fgets(inputline,LINE_LENGTH+2,current_file))
  343.                     numlines++;
  344.  
  345.                 rewind(current_file);
  346.  
  347.                 file_buffer=(short *) malloc(numlines*LINE_LENGTH*sizeof(short));
  348.  
  349.                 if (file_buffer==NULL)
  350.                     return 0;
  351.  
  352.                 for (line=0;line<numlines;line++)
  353.                 {
  354.                     int pos;
  355.  
  356.                     char *tmpptr;
  357.  
  358.                     fgets(inputline,LINE_LENGTH+2,current_file);
  359.  
  360.                     tmpptr=strrchr(inputline,'\n');
  361.  
  362.                     if (tmpptr)
  363.                         *tmpptr=0;
  364.  
  365.                     tmpptr=strrchr(inputline,'\r');
  366.  
  367.                     if (tmpptr)
  368.                         *tmpptr=0;
  369.  
  370.                      for (pos=0;pos<LINE_LENGTH;pos++)
  371.                     {
  372.                         if (pos<strlen(inputline))
  373.                             file_buffer[pos+line*LINE_LENGTH]=(short)inputline[pos];
  374.                         else
  375.                             file_buffer[pos+line*LINE_LENGTH]=0;
  376.                     }
  377.                 }
  378.  
  379.                 fclose(current_file);
  380.  
  381.                 unlink(tmpfilename);
  382.             }
  383.         }
  384.     }
  385.     else
  386.     {
  387.         numlines=1;
  388.  
  389.         current_y=0;
  390.  
  391.         current_page=1;
  392.  
  393.         page_length=66;
  394.  
  395.         tab_size=8;
  396.  
  397.         indent=16;
  398.  
  399.         current_x=16;
  400.  
  401.         left_margin=8;
  402.  
  403.         right_margin=72;
  404.  
  405.         top_blanks=5;
  406.  
  407.         bottom_blanks=5;
  408.  
  409.         line_spacing=1;
  410.  
  411.         strcpy(header,"%");
  412.         strcpy(footer,"");
  413.  
  414.         file_buffer=(short *)malloc(numlines*LINE_LENGTH*sizeof(short));
  415.  
  416.         if (file_buffer==NULL)
  417.             return 0;
  418.  
  419.         memset(file_buffer,0,((unsigned)numlines*LINE_LENGTH*sizeof(short)));
  420.     }
  421.  
  422.     strcpy(filename,input_filename);
  423.  
  424.     changed=0;
  425.  
  426.     return 1;
  427. }
  428.  
  429. void get_user_input()
  430. {
  431.     int quitting=0,inputch;
  432.  
  433.     attron(A_REVERSE);
  434.  
  435.     mvaddstr(0,0," F2:File    F3:Edit    F4:Locate    F5:Options    F6:Spell              F1:Help ");
  436.  
  437.     mvaddstr(22,0,"                                                                                 ");
  438.     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  439.  
  440.     if (insertmode==OVERSTRIKE)
  441.         mvprintw(22,69,"Overstrike");
  442.     else
  443.         mvprintw(22,69,"Insert    ");
  444.  
  445.     if (current_attr&START_BOLD)
  446.         mvprintw(22,64,"Bold");
  447.     else
  448.         mvprintw(22,64,"    ");
  449.         
  450.     if (current_attr&START_UNDERLINE)
  451.         mvprintw(22,54,"Underline");
  452.     else
  453.         mvprintw(22,54,"         ");
  454.  
  455.     attroff(A_REVERSE);
  456.  
  457.     attron(A_BOLD);
  458.  
  459.     mvaddstr(23,0," Press function key to activate menu.  Or press F10 to activate menu bar.      ");
  460.  
  461.     attroff(A_BOLD);
  462.  
  463.     refresh();
  464.  
  465.     display_screen();
  466.  
  467.     while (!quitting)
  468.     {
  469.         inputch=wgetch(textscr);
  470.  
  471.         switch(inputch)
  472.         {
  473.         case 12:    wrefresh(curscr);
  474.  
  475.                 break;
  476.  
  477.         case KEY_IC:    insertmode=(insertmode==INSERT)?OVERSTRIKE:INSERT;
  478.  
  479.                 attron(A_REVERSE);
  480.  
  481.                 mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  482.  
  483.                 if (insertmode==OVERSTRIKE)
  484.                     mvprintw(22,69,"Overstrike");
  485.                 else
  486.                     mvprintw(22,69,"Insert    ");
  487.  
  488.                 if (current_attr&START_BOLD)
  489.                     mvprintw(22,64,"Bold");
  490.                 else
  491.                     mvprintw(22,64,"    ");
  492.         
  493.                 if (current_attr&START_UNDERLINE)
  494.                     mvprintw(22,54,"Underline");
  495.                 else
  496.                     mvprintw(22,54,"         ");
  497.  
  498.                 attroff(A_REVERSE);
  499.  
  500.                 refresh();
  501.  
  502.                 wmove(textscr,current_y,current_x);
  503.  
  504.                 wrefresh(textscr);
  505.  
  506.                 break;
  507.  
  508.         case KEY_F(1):
  509.         case KEY_F(2):
  510.         case KEY_F(3):
  511.         case KEY_F(4):
  512.         case KEY_F(5):
  513.         case KEY_F(6):
  514.                 attron(A_BOLD);
  515.  
  516.                 mvaddstr(23,0," Use the arrow keys to move within a menu or to move to another menu.     ");
  517.  
  518.                 attroff(A_BOLD);
  519.  
  520.                 refresh();
  521.  
  522.                 switch(inputch)
  523.                 {
  524.                     case KEY_F(1):    do_help();
  525.  
  526.                             break;
  527.  
  528.                     case KEY_F(2):    if (file_menu()==1)
  529.                                 quitting++;
  530.  
  531.                             break;
  532.  
  533.                     case KEY_F(3):    edit_menu();
  534.  
  535.                             break;
  536.  
  537.                     case KEY_F(4):    locate_menu();
  538.  
  539.                             break;
  540.  
  541.                     case KEY_F(5):    options_menu();
  542.  
  543.                             break;
  544.  
  545.                     case KEY_F(6):    spell_menu();
  546.  
  547.                             break;
  548.                 }
  549.  
  550.                 attron(A_BOLD);
  551.  
  552.                 mvaddstr(23,0," Press function key to activate menu.  Or press F10 to activate menu bar.      ");
  553.  
  554.                 attroff(A_BOLD);
  555.  
  556.                 attron(A_REVERSE);
  557.  
  558.                 mvaddstr(0,0," F2:File    F3:Edit    F4:Locate    F5:Options    F6:Spell              F1:Help ");
  559.                 mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  560.  
  561.                 if (insertmode==OVERSTRIKE)
  562.                     mvprintw(22,69,"Overstrike");
  563.                 else
  564.                     mvprintw(22,69,"Insert    ");
  565.  
  566.                 if (current_attr&START_BOLD)
  567.                     mvprintw(22,64,"Bold");
  568.                 else
  569.                     mvprintw(22,64,"    ");
  570.         
  571.                 if (current_attr&START_UNDERLINE)
  572.                     mvprintw(22,54,"Underline");
  573.                 else
  574.                     mvprintw(22,54,"         ");
  575.  
  576.                 attroff(A_REVERSE);
  577.  
  578.                 refresh();
  579.  
  580.                 wmove(textscr,current_y,current_x);
  581.  
  582.                 wrefresh(textscr);
  583.  
  584.                 break;
  585.         
  586.         case 27:
  587.         case 274:    attron(A_BOLD);
  588.  
  589.                 mvaddstr(23,0," Use the arrow keys to move within the menubar; Enter to select, Esc to exit.");
  590.  
  591.                 attroff(A_BOLD);
  592.  
  593.                 refresh();
  594.  
  595.                 if (do_menubar()==1)
  596.                     quitting++;
  597.  
  598.                 attron(A_BOLD);
  599.  
  600.                 mvaddstr(23,0," Press function key to activate menu.  Or press F10 to activate menu bar.      ");
  601.  
  602.                 attroff(A_BOLD);
  603.  
  604.                 attron(A_REVERSE);
  605.  
  606.                 mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  607.  
  608.                 if (insertmode==OVERSTRIKE)
  609.                     mvprintw(22,69,"Overstrike");
  610.                 else
  611.                     mvprintw(22,69,"Insert    ");
  612.  
  613.                 if (current_attr&START_BOLD)
  614.                     mvprintw(22,64,"Bold");
  615.                 else
  616.                     mvprintw(22,64,"    ");
  617.         
  618.                 if (current_attr&START_UNDERLINE)
  619.                     mvprintw(22,54,"Underline");
  620.                 else
  621.                     mvprintw(22,54,"         ");
  622.  
  623.                 attroff(A_REVERSE);
  624.  
  625.                 refresh();
  626.  
  627.                 wmove(textscr,current_y,current_x);
  628.  
  629.                 wrefresh(textscr);
  630.  
  631.                 break;
  632.  
  633.         case '\t':    if (insertmode==OVERSTRIKE)
  634.                 {
  635.                     if (current_x<indent)
  636.                         current_x=indent;
  637.                     else
  638.                     {
  639.                         if (current_x%tab_size==0)
  640.                             current_x+=tab_size;
  641.                         else
  642.                             for (;current_x%tab_size;current_x++);
  643.                     }
  644.  
  645.                     wmove(textscr,current_y,current_x);
  646.  
  647.                     wrefresh(textscr);
  648.                 }
  649.                 else
  650.                 {
  651.                     int new_x;
  652.  
  653.                     short tmpstr[LINE_LENGTH];
  654.  
  655.                     changed++;
  656.  
  657.                     if (current_x<indent)
  658.                         new_x=indent;
  659.                     else
  660.                     {
  661.                         if (current_x%tab_size==0)
  662.                             new_x=current_x+tab_size;
  663.                         else
  664.                             for (new_x=current_x;new_x%tab_size;new_x++);
  665.                     }
  666.  
  667.                     memset(tmpstr,0,((unsigned)LINE_LENGTH*2));
  668.  
  669. #if 0
  670.                     memset(tmpstr,' ',((unsigned)new_x-current_x));
  671. #endif
  672.  
  673.                     insert(tmpstr,new_x-current_x,current_y);
  674.  
  675.                     current_x=new_x;
  676.  
  677.                     display_screen();
  678.                 }
  679.  
  680.                 break;
  681.  
  682.         case '\r':
  683.         case '\n':    if (insertmode==INSERT)
  684.                 {
  685.                     int pos;
  686.  
  687.                     changed++;
  688.  
  689.                     numlines+=(1+line_spacing);
  690.             
  691.                     file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  692.                             
  693.                     if (file_buffer==NULL)
  694.                         my_exit("Realloc error on file buffer.\n");
  695.             
  696.                     memset(file_buffer+(numlines-(1+line_spacing))*LINE_LENGTH,0,((unsigned)(1+line_spacing)*LINE_LENGTH*sizeof(short)));
  697.             
  698.                     if ((current_y+(current_page-1)*LINES_PER_PAGE+(1+line_spacing))<(numlines-(1+line_spacing)))
  699.                     {
  700.                         int line;
  701.  
  702.                         for (line=numlines-1;line>=current_y+2*(1+line_spacing)+(current_page-1)*LINES_PER_PAGE;line--)
  703.                             memcpy(file_buffer+line*LINE_LENGTH,file_buffer+(line-(1+line_spacing))*LINE_LENGTH,LINE_LENGTH*sizeof(short));
  704.  
  705.                         memset(file_buffer+(current_y+(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,0,((unsigned)80*sizeof(short)));
  706.                     }
  707.  
  708.                     for (pos=current_x;pos<=right_margin;pos++)
  709.                     {
  710.                         file_buffer[pos+left_margin-current_x+(current_y+(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=
  711.                                 file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  712.  
  713.                         file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=0;
  714.                     }
  715.  
  716.                     display_screen();
  717.                 }
  718.  
  719.                 current_x=right_margin;
  720.  
  721.         case KEY_RIGHT:    if (current_x<right_margin)
  722.                 {
  723.                     current_x++;
  724.  
  725.                     wmove(textscr,current_y,current_x);
  726.  
  727.                     wrefresh(textscr);
  728.  
  729.                     break;
  730.                 }
  731.                 else
  732.                 {
  733.                     current_x=left_margin;
  734.                 }
  735.         case KEY_DOWN:    if (current_y+(1+line_spacing)+((current_page-1)*LINES_PER_PAGE)>=numlines)
  736.                 {
  737.                     numlines+=line_spacing+1;
  738.     
  739.                     file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  740.                 
  741.                     if (file_buffer==NULL)
  742.                         my_exit("Realloc error on file buffer.\n");
  743.  
  744.                     memset(file_buffer+(numlines-(1+line_spacing))*LINE_LENGTH,0,((unsigned)(1+line_spacing)*LINE_LENGTH*sizeof(short)));
  745.                 }
  746.  
  747.                 wattroff(textscr,A_BOLD);
  748.  
  749.                 wattroff(textscr,A_UNDERLINE);
  750.  
  751.                 if (current_y<(LINES_PER_PAGE-(1+line_spacing)))
  752.                 {
  753.                     current_y=(current_y+1+line_spacing)%LINES_PER_PAGE;
  754.  
  755.                     attron(A_REVERSE);
  756.  
  757.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  758.  
  759.                     if (insertmode==OVERSTRIKE)
  760.                         mvprintw(22,69,"Overstrike");
  761.                     else
  762.                         mvprintw(22,69,"Insert    ");
  763.     
  764.                     if (current_attr&START_BOLD)
  765.                         mvprintw(22,64,"Bold");
  766.                     else
  767.                         mvprintw(22,64,"    ");
  768.             
  769.                     if (current_attr&START_UNDERLINE)
  770.                         mvprintw(22,54,"Underline");
  771.                     else
  772.                         mvprintw(22,54,"         ");
  773.  
  774.                     attroff(A_REVERSE);
  775.  
  776.                     refresh();
  777.  
  778.                     wmove(textscr,current_y,current_x);
  779.  
  780.                     wrefresh(textscr);
  781.  
  782.                     break;
  783.                 }
  784.  
  785.         case KEY_NPAGE:    if ((current_page*LINES_PER_PAGE)<=numlines)
  786.                 {
  787.                     current_page++;
  788.  
  789.                     wattroff(textscr,A_BOLD);
  790.  
  791.                     wattroff(textscr,A_UNDERLINE);
  792.  
  793.                     if (inputch==KEY_NPAGE)
  794.                     {
  795.                         if (line_spacing>0)
  796.                             current_y=((current_page-1)*LINES_PER_PAGE)%(1+line_spacing);
  797.                         else
  798.                             current_y=0;
  799.                     }
  800.                     else
  801.                         current_y=(current_y+1+line_spacing)%LINES_PER_PAGE;
  802.  
  803.                     attron(A_REVERSE);
  804.  
  805.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  806.  
  807.                     if (insertmode==OVERSTRIKE)
  808.                         mvprintw(22,69,"Overstrike");
  809.                     else
  810.                         mvprintw(22,69,"Insert    ");
  811.     
  812.                     if (current_attr&START_BOLD)
  813.                         mvprintw(22,64,"Bold");
  814.                     else
  815.                         mvprintw(22,64,"    ");
  816.             
  817.                     if (current_attr&START_UNDERLINE)
  818.                         mvprintw(22,54,"Underline");
  819.                     else
  820.                         mvprintw(22,54,"         ");
  821.  
  822.                     attroff(A_REVERSE);
  823.  
  824.                     refresh();
  825.  
  826.                     display_screen();
  827.                 }
  828.  
  829.                 break;
  830.  
  831.         case 127:
  832.         case KEY_DC:    {
  833.                     int pos;
  834.  
  835.                     changed++;
  836.  
  837.                     for (pos=current_x;pos<right_margin;pos++)
  838.                         file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=file_buffer[pos+1+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  839.  
  840.                     file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=0;
  841.  
  842.                     justify(current_y);
  843.                 }
  844.  
  845.                 display_screen();
  846.  
  847.                 break;
  848.  
  849.         case '\b':
  850.         case KEY_BACKSPACE:
  851.                 if (current_x<=left_margin&&(current_y!=0||current_page!=1))
  852.                 {
  853.                     int pos;
  854.  
  855.                     for (pos=right_margin;pos>=left_margin;pos--)
  856.                         if (file_buffer[pos+(current_y-(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  857.                                 file_buffer[pos+(current_y-(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  858.                             break;
  859.  
  860.                     current_x=-(pos+1);
  861.                         
  862.                     changed++;
  863.  
  864.                     justify(current_y-(1+line_spacing));
  865.                 }
  866.                 else if (current_x>left_margin)
  867.                 {
  868.                     int pos;
  869.  
  870.                     changed++;
  871.  
  872.                     for (pos=current_x-1;pos<right_margin;pos++)
  873.                         file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=file_buffer[pos+1+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  874.  
  875.                     file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=0;
  876.  
  877.                     justify(current_y);
  878.                 }
  879.  
  880.                 display_screen();
  881.  
  882.         case KEY_LEFT:    if (current_x>left_margin)
  883.                 {
  884.                     current_x--;
  885.  
  886.                     wmove(textscr,current_y,current_x);
  887.  
  888.                     wrefresh(textscr);
  889.  
  890.                     break;
  891.                 }
  892.                 else if ((current_page>1||current_y!=0)&¤t_x>=0)
  893.                 {
  894.                     current_x=right_margin;
  895.                 }
  896.                 else if (current_x<0)
  897.                     current_x=-(current_x);
  898.                 else
  899.                     break;
  900.  
  901.         case KEY_UP:    if ((line_spacing==0&¤t_y>0)||(line_spacing>0&¤t_y>(((current_page-1)*LINES_PER_PAGE))%(1+line_spacing)))
  902.                 {
  903.                     current_y=current_y-1-line_spacing;
  904.  
  905.                     wattroff(textscr,A_BOLD);
  906.  
  907.                     wattroff(textscr,A_UNDERLINE);
  908.  
  909.                     attron(A_REVERSE);
  910.  
  911.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  912.  
  913.                     if (insertmode==OVERSTRIKE)
  914.                         mvprintw(22,69,"Overstrike");
  915.                     else
  916.                         mvprintw(22,69,"Insert    ");
  917.     
  918.                     if (current_attr&START_BOLD)
  919.                         mvprintw(22,64,"Bold");
  920.                     else
  921.                         mvprintw(22,64,"    ");
  922.             
  923.                     if (current_attr&START_UNDERLINE)
  924.                         mvprintw(22,54,"Underline");
  925.                     else
  926.                         mvprintw(22,54,"         ");
  927.  
  928.                     attroff(A_REVERSE);
  929.  
  930.                     refresh();
  931.  
  932.                     wmove(textscr,current_y,current_x);
  933.  
  934.                     wrefresh(textscr);
  935.  
  936.                     break;
  937.                 }
  938.  
  939.         case KEY_PPAGE:    if (current_page>1)
  940.                 {
  941.                     current_page--;
  942.  
  943.                     wattroff(textscr,A_BOLD);
  944.  
  945.                     wattroff(textscr,A_UNDERLINE);
  946.  
  947.                     if (inputch==KEY_PPAGE)
  948.                     {
  949.                         if (line_spacing>0)
  950.                             current_y=((current_page)*LINES_PER_PAGE)%(1+line_spacing)-(1+line_spacing)+LINES_PER_PAGE;
  951. #if o
  952.                             current_y=LINES_PER_PAGE-1+((current_page-1)*LINES_PER_PAGE+1)%(1+line_spacing);
  953. #endif
  954.                         else
  955.                             current_y=LINES_PER_PAGE-1;
  956.                     }
  957.                     else
  958.                         current_y=current_y-1-line_spacing;
  959.  
  960.                     if (current_y<0)
  961.                         current_y+=LINES_PER_PAGE;
  962.  
  963.                     attron(A_REVERSE);
  964.  
  965.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  966.  
  967.                     if (insertmode==OVERSTRIKE)
  968.                         mvprintw(22,69,"Overstrike");
  969.                     else
  970.                         mvprintw(22,69,"Insert    ");
  971.     
  972.                     if (current_attr&START_BOLD)
  973.                         mvprintw(22,64,"Bold");
  974.                     else
  975.                         mvprintw(22,64,"    ");
  976.             
  977.                     if (current_attr&START_UNDERLINE)
  978.                         mvprintw(22,54,"Underline");
  979.                     else
  980.                         mvprintw(22,54,"         ");
  981.  
  982.                     attroff(A_REVERSE);
  983.  
  984.                     refresh();
  985.  
  986.                     display_screen();
  987.                 }
  988.  
  989.                 break;
  990.  
  991.         case KEY_HOME:    current_x=left_margin;
  992.  
  993.                 wmove(textscr,current_y,current_x);
  994.  
  995.                 wrefresh(textscr);
  996.  
  997.                 break;
  998.         }
  999.  
  1000.         if (isprint(0xff&inputch)&&!(inputch&0xff00))
  1001.         {
  1002.             short tempstr[LINE_LENGTH];
  1003.  
  1004.             int count=0,pos,word_wrap=0;
  1005.  
  1006.             changed++;
  1007.  
  1008.             if (insertmode==OVERSTRIKE)
  1009.             {
  1010.                 file_buffer[current_x+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=inputch|current_attr;
  1011.  
  1012.                 if (current_attr&START_BOLD)
  1013.                     wattron(textscr,A_BOLD);
  1014.  
  1015.                 if (current_attr&START_UNDERLINE)
  1016.                     wattron(textscr,A_UNDERLINE);
  1017.  
  1018.                 if (current_attr&END_BOLD)
  1019.                     wattroff(textscr,A_BOLD);
  1020.  
  1021.                 if (current_attr&END_UNDERLINE)
  1022.                     wattroff(textscr,A_UNDERLINE);
  1023.  
  1024.                 if (current_attr&START_FOOTNOTE)
  1025.                     wattron(textscr,A_REVERSE);
  1026.  
  1027.                 if (current_attr&END_FOOTNOTE)
  1028.                     wattroff(textscr,A_REVERSE);
  1029.  
  1030.                 current_attr=NORMAL;
  1031.  
  1032.                 waddch(textscr,inputch);
  1033.  
  1034.                  if (current_x<right_margin)
  1035.                 {
  1036.                     current_x++;
  1037.  
  1038.                     wmove(textscr,current_y,current_x);
  1039.  
  1040.                     wrefresh(textscr);
  1041.  
  1042.                     continue;
  1043.                 }
  1044.                 else
  1045.                 {
  1046.                     /* do word wrap */
  1047.  
  1048.                     for (pos=current_x;pos>=left_margin;pos--)
  1049.                     {
  1050.                         if (((char)file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH])==' '||(((char)file_buffer[pos+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH])==0))
  1051.                             break;
  1052.  
  1053.                         count++;
  1054.                     }
  1055.  
  1056.                     count--;
  1057.  
  1058.                     if (pos<left_margin||pos==current_x)
  1059.                         current_x=left_margin;
  1060.                     else
  1061.                     /* wrap word */
  1062.                     {
  1063.                         word_wrap=1;
  1064.  
  1065.                         memcpy(tempstr,file_buffer+current_x-count+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,(count+1)*sizeof(short));
  1066.  
  1067.                         memset(file_buffer+current_x-count+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,0,((unsigned)(count+1)*sizeof(short)));
  1068.  
  1069.                         current_x=left_margin+count+1;
  1070.                     }
  1071.                 }
  1072.  
  1073.                 if (current_y+(1+line_spacing)+((current_page-1)*LINES_PER_PAGE)>=numlines)
  1074.                 {
  1075.                     numlines+=line_spacing+1;
  1076.     
  1077.                     file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  1078.                 
  1079.                     if (file_buffer==NULL)
  1080.                         my_exit("Realloc error on file buffer.\n");
  1081.  
  1082.                     memset(file_buffer+(numlines-(1+line_spacing))*LINE_LENGTH,0,((unsigned)(1+line_spacing)*LINE_LENGTH*sizeof(short)));
  1083.                 }
  1084.  
  1085.                 if (current_y<(LINES_PER_PAGE-(1+line_spacing)))
  1086.                 {
  1087.                     current_y+=1+line_spacing;
  1088.  
  1089.                     wattroff(textscr,A_BOLD);
  1090.  
  1091.                     wattroff(textscr,A_UNDERLINE);
  1092.  
  1093.                     if (word_wrap)
  1094.                     {
  1095.                         memcpy(file_buffer+left_margin+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,tempstr,(count+1)*sizeof(short));
  1096.     
  1097.                         display_screen();
  1098.                     }
  1099.  
  1100.                     attron(A_REVERSE);
  1101.  
  1102.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  1103.  
  1104.                     if (insertmode==OVERSTRIKE)
  1105.                         mvprintw(22,69,"Overstrike");
  1106.                     else
  1107.                         mvprintw(22,69,"Insert    ");
  1108.     
  1109.                     if (current_attr&START_BOLD)
  1110.                         mvprintw(22,64,"Bold");
  1111.                     else
  1112.                         mvprintw(22,64,"    ");
  1113.             
  1114.                     if (current_attr&START_UNDERLINE)
  1115.                         mvprintw(22,54,"Underline");
  1116.                     else
  1117.                         mvprintw(22,54,"         ");
  1118.  
  1119.                     attroff(A_REVERSE);
  1120.  
  1121.                     refresh();
  1122.  
  1123.                     wmove(textscr,current_y,current_x);
  1124.  
  1125.                     wrefresh(textscr);
  1126.  
  1127.                     continue;
  1128.                 }
  1129.  
  1130.                 if ((current_page*LINES_PER_PAGE)<=numlines)
  1131.                 {
  1132.                     current_y=(current_y+1+line_spacing)%LINES_PER_PAGE;
  1133.  
  1134.                     wattroff(textscr,A_BOLD);
  1135.  
  1136.                     wattroff(textscr,A_UNDERLINE);
  1137.  
  1138.                     current_page++;
  1139.  
  1140.                     if (word_wrap)
  1141.                         memcpy(file_buffer+left_margin+(current_y+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,tempstr,(count+1)*sizeof(short));
  1142.  
  1143.                     attron(A_REVERSE);
  1144.  
  1145.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  1146.  
  1147.                     if (insertmode==OVERSTRIKE)
  1148.                         mvprintw(22,69,"Overstrike");
  1149.                     else
  1150.                         mvprintw(22,69,"Insert    ");
  1151.     
  1152.                     if (current_attr&START_BOLD)
  1153.                         mvprintw(22,64,"Bold");
  1154.                     else
  1155.                         mvprintw(22,64,"    ");
  1156.             
  1157.                     if (current_attr&START_UNDERLINE)
  1158.                         mvprintw(22,54,"Underline");
  1159.                     else
  1160.                         mvprintw(22,54,"         ");
  1161.  
  1162.                     attroff(A_REVERSE);
  1163.  
  1164.                     refresh();
  1165.  
  1166.                     display_screen();
  1167.                 }
  1168.             }
  1169.             else
  1170.             {
  1171.                 /* build a string of one short which is the
  1172.                    character typed and the current attr; then
  1173.                    insert() that short, set the cursor position
  1174.                    appropriately and display the screen */
  1175.  
  1176.                 tempstr[0]=inputch|current_attr;
  1177.  
  1178.                 current_attr=NORMAL;
  1179.  
  1180.                 insert(tempstr,1,current_y);
  1181.  
  1182.                 display_screen();
  1183.  
  1184.                 if (current_x<0)
  1185.                     current_x=-current_x;
  1186.                 else if (current_x<=right_margin)
  1187.                     continue;
  1188.  
  1189.                 if (current_y<(LINES_PER_PAGE-(1+line_spacing)))
  1190.                 {
  1191.                     current_y+=1+line_spacing;
  1192.  
  1193.                     wattroff(textscr,A_BOLD);
  1194.  
  1195.                     wattroff(textscr,A_UNDERLINE);
  1196.  
  1197.                     attron(A_REVERSE);
  1198.  
  1199.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  1200.  
  1201.                     if (insertmode==OVERSTRIKE)
  1202.                         mvprintw(22,69,"Overstrike");
  1203.                     else
  1204.                         mvprintw(22,69,"Insert    ");
  1205.     
  1206.                     if (current_attr&START_BOLD)
  1207.                         mvprintw(22,64,"Bold");
  1208.                     else
  1209.                         mvprintw(22,64,"    ");
  1210.             
  1211.                     if (current_attr&START_UNDERLINE)
  1212.                         mvprintw(22,54,"Underline");
  1213.                     else
  1214.                         mvprintw(22,54,"         ");
  1215.  
  1216.                     attroff(A_REVERSE);
  1217.  
  1218.                     refresh();
  1219.  
  1220.                     wmove(textscr,current_y,current_x);
  1221.  
  1222.                     wrefresh(textscr);
  1223.  
  1224.                     continue;
  1225.                 }
  1226.  
  1227.                 if ((current_page*LINES_PER_PAGE)<=numlines)
  1228.                 {
  1229.                     current_y=(current_y+1+line_spacing)%LINES_PER_PAGE;
  1230.  
  1231.                     wattroff(textscr,A_BOLD);
  1232.     
  1233.                     wattroff(textscr,A_UNDERLINE);
  1234.  
  1235.                     current_page++;
  1236.  
  1237.                     attron(A_REVERSE);
  1238.  
  1239.                     mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  1240.  
  1241.                     if (insertmode==OVERSTRIKE)
  1242.                         mvprintw(22,69,"Overstrike");
  1243.                     else
  1244.                         mvprintw(22,69,"Insert    ");
  1245.     
  1246.                     if (current_attr&START_BOLD)
  1247.                         mvprintw(22,64,"Bold");
  1248.                     else
  1249.                         mvprintw(22,64,"    ");
  1250.             
  1251.                     if (current_attr&START_UNDERLINE)
  1252.                         mvprintw(22,54,"Underline");
  1253.                     else
  1254.                         mvprintw(22,54,"         ");
  1255.  
  1256.                     attroff(A_REVERSE);
  1257.  
  1258.                     refresh();
  1259.  
  1260.                     display_screen();
  1261.                 }
  1262.             }
  1263.         }
  1264.     }
  1265. }
  1266.  
  1267. void display_screen()
  1268. {
  1269.     int row,col;
  1270.  
  1271.     werase(textscr);
  1272.  
  1273.     for (row=0;row<LINES_PER_PAGE;row++)
  1274.     {
  1275.         wattrset(textscr,A_NORMAL);
  1276.  
  1277.         for (col=0;col<80;col++)
  1278.         {
  1279.             if ((row+(current_page-1)*LINES_PER_PAGE)>=numlines)
  1280.                 continue;
  1281.  
  1282.             wmove(textscr,row,col);
  1283.  
  1284.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&START_BOLD)
  1285.                 wattron(textscr,A_BOLD);
  1286.  
  1287.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&END_BOLD)
  1288.                 wattroff(textscr,A_BOLD);
  1289.  
  1290.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&START_UNDERLINE)
  1291.                 wattron(textscr,A_UNDERLINE);
  1292.  
  1293.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&END_UNDERLINE)
  1294.                 wattroff(textscr,A_UNDERLINE);
  1295.  
  1296.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&START_FOOTNOTE)
  1297.                 wattron(textscr,A_REVERSE);
  1298.  
  1299.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&END_FOOTNOTE)
  1300.                 wattroff(textscr,A_REVERSE);
  1301.  
  1302.             if (file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&0xff)
  1303.                 waddch(textscr,((int)file_buffer[col+(row+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]&0xff));
  1304.         }
  1305.     }
  1306.             
  1307.     wmove(textscr,current_y,current_x);
  1308.  
  1309.     wattrset(textscr,A_NORMAL);
  1310.  
  1311.     wrefresh(textscr);
  1312. }
  1313.  
  1314. void my_exit(message)
  1315. char *message;
  1316. {
  1317.     erase();
  1318.  
  1319.     refresh();
  1320.  
  1321.     end_curses();
  1322.  
  1323.     fputs(message,stderr);
  1324.  
  1325.     exit(1);
  1326. }
  1327.  
  1328. void write_file()
  1329. {
  1330.     char output_filename[75]="";
  1331.  
  1332.     int retval;
  1333.  
  1334.     output_filename[68]=0;
  1335.  
  1336.     if (strstr(filename,".hwp"))
  1337.         strcpy(output_filename,filename);
  1338.  
  1339.     retval=get_inputline("Enter filename to write (ASCII unless ending in .hwp):",output_filename,output_filename);
  1340.  
  1341.     if (retval!=-1)
  1342.     {
  1343.         if (strstr(output_filename,".hwp"))
  1344.             do_write_file(output_filename);
  1345.         else
  1346.             write_ascii(output_filename);
  1347.     }
  1348. }
  1349.  
  1350. int get_inputline(message,prompt,output)
  1351. char *message,*prompt,*output;
  1352. {
  1353.     WINDOW *message_win;
  1354.  
  1355.     char tmpstr[69];
  1356.  
  1357.     int inputch=0,pos=0;
  1358.  
  1359.     memset(output+strlen(output),' ',((unsigned)68-strlen(output)));
  1360.  
  1361.     if (output!=prompt)
  1362.         memset(prompt+strlen(prompt),' ',((unsigned)68-strlen(prompt)));
  1363.  
  1364.     output[68]=0;
  1365.  
  1366.     message_win=newwin(7,70,10,5);
  1367.  
  1368.     keypad(message_win,TRUE);
  1369.  
  1370.     mybox(message_win,7,70);
  1371.  
  1372.     mvwaddstr(message_win,1,1,message);
  1373.  
  1374.     wattron(message_win,A_REVERSE);
  1375.  
  1376.     mvwaddstr(message_win,3,1,prompt);
  1377.  
  1378.     wattroff(message_win,A_REVERSE);
  1379.  
  1380.     mvwaddstr(message_win,5,25,"CTRL-C to Cancel");
  1381.  
  1382.     wmove(message_win,3,1);
  1383.  
  1384.     wrefresh(message_win);
  1385.  
  1386.     wattron(message_win,A_REVERSE);
  1387.  
  1388.     while(inputch!='\n'&&inputch!='\r'&&inputch!=3)
  1389.     {
  1390.         inputch=wgetch(message_win);
  1391.  
  1392.         switch(inputch)
  1393.         {
  1394.         case 12:    wrefresh(curscr);
  1395.  
  1396.                 break;
  1397.  
  1398.         case KEY_IC:    insertmode=(insertmode==INSERT)?OVERSTRIKE:INSERT;
  1399.  
  1400.                 mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  1401.  
  1402.                 if (insertmode==OVERSTRIKE)
  1403.                     mvprintw(22,69,"Overstrike");
  1404.                 else
  1405.                     mvprintw(22,69,"Insert    ");
  1406.  
  1407.                 if (current_attr&START_BOLD)
  1408.                     mvprintw(22,64,"Bold");
  1409.                 else
  1410.                     mvprintw(22,64,"    ");
  1411.         
  1412.                 if (current_attr&START_UNDERLINE)
  1413.                     mvprintw(22,54,"Underline");
  1414.                 else
  1415.                     mvprintw(22,54,"         ");
  1416.  
  1417.                 break;
  1418.  
  1419.         case KEY_LEFT:    if (pos>0)
  1420.                 {
  1421.                     pos--;
  1422.  
  1423.                     wmove(message_win,3,pos+1);
  1424.  
  1425.                     wrefresh(message_win);
  1426.                 }
  1427.  
  1428.                 break;
  1429.  
  1430.         case KEY_RIGHT:    if (pos<67)
  1431.                 {
  1432.                     pos++;
  1433.  
  1434.                     wmove(message_win,3,pos+1);
  1435.  
  1436.                     wrefresh(message_win);
  1437.                 }
  1438.  
  1439.                 break;
  1440.  
  1441.         case 127:
  1442.         case KEY_DC:    {
  1443.                     int pos1;
  1444.  
  1445.                     for (pos1=pos;pos1<68;pos1++)
  1446.                         output[pos1]=output[pos1+1];
  1447.                 }
  1448.  
  1449.                 mvwaddstr(message_win,3,1,output);
  1450.  
  1451.                 wmove(message_win,3,pos+1);
  1452.  
  1453.                 wrefresh(message_win);
  1454.  
  1455.                 break;
  1456.  
  1457.         case '\b':
  1458.         case KEY_BACKSPACE:
  1459.                 {
  1460.                     int pos1;
  1461.  
  1462.                     for (pos1=pos-1;pos1<68;pos1++)
  1463.                         output[pos1]=output[pos1+1];
  1464.                 }
  1465.  
  1466.                 mvwaddstr(message_win,3,1,output);
  1467.  
  1468.                 pos--;
  1469.  
  1470.                 wmove(message_win,3,pos+1);
  1471.  
  1472.                 wrefresh(message_win);
  1473.  
  1474.                 break;
  1475.  
  1476.         case KEY_HOME:    pos=0;
  1477.  
  1478.                 wmove(message_win,3,pos+1);
  1479.  
  1480.                 wrefresh(message_win);
  1481.  
  1482.                 break;
  1483.  
  1484.         case KEY_F(1):    wattroff(message_win,A_REVERSE);
  1485.  
  1486.                 werase(message_win);
  1487.  
  1488.                 wrefresh(message_win);
  1489.  
  1490.                 do_help();
  1491.  
  1492.                 mybox(message_win,7,70);
  1493.  
  1494.                 mvwaddstr(message_win,1,1,message);
  1495.  
  1496.                 wattron(message_win,A_REVERSE);
  1497.  
  1498.                 mvwaddstr(message_win,3,1,output);
  1499.  
  1500.                 wattroff(message_win,A_REVERSE);
  1501.  
  1502.                 mvwaddstr(message_win,5,25,"CTRL-C to Cancel");
  1503.  
  1504.                 wmove(message_win,3,1+pos);
  1505.  
  1506.                 wrefresh(message_win);
  1507.  
  1508.                 wattron(message_win,A_REVERSE);
  1509.  
  1510.                 break;
  1511.         }
  1512.  
  1513.         if (isprint(0xff&inputch)&&!(inputch&0xff00))
  1514.         {
  1515.             if (insertmode==OVERSTRIKE)
  1516.             {
  1517.                 output[pos]=inputch;
  1518.  
  1519.                 waddch(message_win,inputch);
  1520.  
  1521.                 if (pos<67)
  1522.                     pos++;
  1523.                 else
  1524.                     beep();
  1525.  
  1526.                 wmove(message_win,3,pos+1);
  1527.  
  1528.                 wrefresh(message_win);
  1529.             }
  1530.             else
  1531.             {
  1532.                 if (output[67]!=' '&&output[67]!=0)
  1533.                     beep();
  1534.                 else
  1535.                 {
  1536.                     int pos1;
  1537.  
  1538.                     for (pos1=67;pos1>pos;pos1--)
  1539.                         output[pos1]=output[pos1-1];
  1540.  
  1541.                     output[pos]=inputch;
  1542.  
  1543.                     if (pos<67)
  1544.                         pos++;
  1545.                     else
  1546.                         beep();
  1547.     
  1548.                     mvwaddstr(message_win,3,1,output);
  1549.  
  1550.                     wmove(message_win,3,pos+1);
  1551.             
  1552.                     wrefresh(message_win);
  1553.                 }
  1554.             }
  1555.         }
  1556.     }
  1557.  
  1558.     wattroff(message_win,A_REVERSE);
  1559.  
  1560.     werase(message_win);
  1561.  
  1562.     wrefresh(message_win);
  1563.  
  1564.     delwin(message_win);
  1565.  
  1566.     display_screen();
  1567.  
  1568.     strcpy(tmpstr,output+strspn(output," "));
  1569.  
  1570.     for (pos=strlen(tmpstr)-1;pos>0;pos--)
  1571.         if (tmpstr[pos]==' ')
  1572.             tmpstr[pos]=0;
  1573.         else
  1574.             break;
  1575.  
  1576.     strcpy(output,tmpstr);
  1577.  
  1578.     if (inputch==3)
  1579.         return -1;
  1580.     else
  1581.         return 0;
  1582. }
  1583.  
  1584. void error_message(title,message)
  1585. char *title,*message;
  1586. {
  1587.     WINDOW *error_win;
  1588.  
  1589.     error_win=newwin(7,70,10,5);
  1590.  
  1591.     mybox(error_win,7,70);
  1592.  
  1593.     mvwaddstr(error_win,1,1,title);
  1594.  
  1595.     mvwaddstr(error_win,3,1,message);
  1596.  
  1597.     mvwaddstr(error_win,5,25,"Press any key to go on...");
  1598.  
  1599.     wrefresh(error_win);
  1600.  
  1601.     while (wgetch(error_win)==12)
  1602.         wrefresh(curscr);
  1603.  
  1604.     werase(error_win);
  1605.  
  1606.     wrefresh(error_win);
  1607.  
  1608.     delwin(error_win);
  1609.  
  1610.     display_screen();
  1611. }
  1612.  
  1613. int file_menu()
  1614. {
  1615.     WINDOW *file_win;
  1616.  
  1617.     int inputch=0,pos=0;
  1618.  
  1619.     file_win=newwin(7,12,1,0);
  1620.  
  1621.     mybox(file_win,7,12);
  1622.  
  1623.     wattron(file_win,A_REVERSE);
  1624.  
  1625.     mvwaddstr(file_win,1,1,"Load File");
  1626.  
  1627.     wattroff(file_win,A_REVERSE);
  1628.  
  1629.     mvwaddstr(file_win,2,1,"Save File");
  1630.  
  1631.     mvwaddstr(file_win,3,1,"Print File");
  1632.  
  1633.     mvwaddstr(file_win,4,1,"New");
  1634.  
  1635.     mvwaddstr(file_win,5,1,"Quit");
  1636.  
  1637.     keypad(file_win,TRUE);
  1638.  
  1639.     wmove(file_win,1,1);
  1640.  
  1641.     wrefresh(file_win);
  1642.  
  1643.     while(inputch!='\n'&&inputch!='\r'&&inputch!=27)
  1644.     {
  1645.         inputch=getch();
  1646.  
  1647.         switch(pos)
  1648.         {
  1649.             case 0:    mvwaddstr(file_win,1,1,"Load File");
  1650.  
  1651.                 break;
  1652.  
  1653.             case 1: mvwaddstr(file_win,2,1,"Save File");
  1654.  
  1655.                 break;
  1656.  
  1657.             case 2: mvwaddstr(file_win,3,1,"Print File");
  1658.  
  1659.                 break;
  1660.  
  1661.             case 3: mvwaddstr(file_win,4,1,"New");
  1662.  
  1663.                 break;
  1664.  
  1665.             case 4: mvwaddstr(file_win,5,1,"Quit");
  1666.  
  1667.                 break;
  1668.         }
  1669.  
  1670.         switch(inputch)
  1671.         {
  1672.             case 12:    wrefresh(curscr);
  1673.  
  1674.                     break;
  1675.  
  1676.             case KEY_DOWN:    pos++;
  1677.  
  1678.                     break;
  1679.  
  1680.             case KEY_UP:    pos--;
  1681.  
  1682.                     if(pos<0)
  1683.                         pos+=5;
  1684.  
  1685.                     break;
  1686.  
  1687.             case KEY_RIGHT:    werase(file_win);
  1688.  
  1689.                     wrefresh(file_win);
  1690.  
  1691.                     delwin(file_win);
  1692.  
  1693.                     display_screen();
  1694.  
  1695.                     return -1;
  1696.  
  1697.             case KEY_LEFT:    werase(file_win);
  1698.  
  1699.                     wrefresh(file_win);
  1700.  
  1701.                     delwin(file_win);
  1702.  
  1703.                     display_screen();
  1704.  
  1705.                     return -2;
  1706.         }
  1707.  
  1708.         pos=pos%5;
  1709.  
  1710.         wattron(file_win,A_REVERSE);
  1711.  
  1712.         switch(pos)
  1713.         {
  1714.             case 0:    mvwaddstr(file_win,1,1,"Load File");
  1715.                 wmove(file_win,1,1);
  1716.  
  1717.                 break;
  1718.  
  1719.             case 1: mvwaddstr(file_win,2,1,"Save File");
  1720.                 wmove(file_win,2,1);
  1721.  
  1722.                 break;
  1723.  
  1724.             case 2: mvwaddstr(file_win,3,1,"Print File");
  1725.                 wmove(file_win,3,1);
  1726.  
  1727.                 break;
  1728.  
  1729.             case 3: mvwaddstr(file_win,4,1,"New");
  1730.                 wmove(file_win,4,1);
  1731.  
  1732.                 break;
  1733.  
  1734.             case 4: mvwaddstr(file_win,5,1,"Quit");
  1735.                 wmove(file_win,5,1);
  1736.  
  1737.                 break;
  1738.         }
  1739.  
  1740.         wattroff(file_win,A_REVERSE);
  1741.  
  1742.         wrefresh(file_win);
  1743.     }
  1744.  
  1745.     werase(file_win);
  1746.  
  1747.     wrefresh(file_win);
  1748.  
  1749.     delwin(file_win);
  1750.  
  1751.     display_screen();
  1752.  
  1753.     if (inputch==27)
  1754.         return 0;
  1755.  
  1756.     attron(A_REVERSE);
  1757.  
  1758.     mvaddstr(0,0,"                                                                        F1:Help ");
  1759.  
  1760.     attroff(A_REVERSE);
  1761.  
  1762.     refresh();
  1763.  
  1764.     switch(pos)
  1765.     {
  1766.         case 0:    load_file();
  1767.  
  1768.             display_screen();
  1769.  
  1770.             return 0;
  1771.  
  1772.         case 1:    write_file();
  1773.  
  1774.             return 0;
  1775.  
  1776.         case 2: print_file();
  1777.  
  1778.             return 0;
  1779.  
  1780.         case 3:    if (changed)
  1781.             {
  1782.                 if (confirm_load()==0)
  1783.                     return 0;
  1784.             }
  1785.  
  1786.             unlink("untitled.hwp");
  1787.  
  1788.             do_load_file("untitled.hwp");
  1789.  
  1790.             display_screen();
  1791.  
  1792.             return 0;
  1793.  
  1794.         case 4:    if (confirm_quit())
  1795.                 return 1;
  1796.             else
  1797.                 return 0;
  1798.     }
  1799. }
  1800.  
  1801. void do_write_file(output_filename)
  1802. char *output_filename;
  1803. {
  1804.     int write_fd,pad;
  1805.  
  1806.     errno=0;
  1807.  
  1808.     if (strlen(output_filename)>5)
  1809.     {
  1810.         if (strcmp(output_filename+strlen(output_filename)-4,".hwp"))
  1811.             strcat(output_filename,".hwp");
  1812.     }
  1813.     else
  1814.         strcat(output_filename,".hwp");
  1815.  
  1816.     write_fd=open(output_filename,O_WRONLY|O_CREAT,0666);
  1817.  
  1818.     if (write_fd==-1)
  1819.     {
  1820.         error_message("Couldn't write file because:",strerror(errno));
  1821.  
  1822.         return;
  1823.     }
  1824.  
  1825.     write(write_fd,&version,sizeof(int));
  1826.     write(write_fd,&numlines,sizeof(int));
  1827.     write(write_fd,&page_length,sizeof(int));
  1828.     write(write_fd,&tab_size,sizeof(int));
  1829.     write(write_fd,&indent,sizeof(int));
  1830.     write(write_fd,&left_margin,sizeof(int));
  1831.     write(write_fd,&right_margin,sizeof(int));
  1832.     write(write_fd,&top_blanks,sizeof(int));
  1833.     write(write_fd,&bottom_blanks,sizeof(int));
  1834.     write(write_fd,&starting_page_no,sizeof(int));
  1835.     write(write_fd,&line_spacing,sizeof(int));
  1836.     write(write_fd,¤t_page,sizeof(int));
  1837.     write(write_fd,¤t_y,sizeof(int));
  1838.  
  1839.     for (pad=0;pad<12;pad++)
  1840.         write(write_fd,0,sizeof(int));
  1841.  
  1842.     write(write_fd,header,LINE_LENGTH+1);
  1843.     write(write_fd,footer,LINE_LENGTH+1);
  1844.  
  1845.     write(write_fd,file_buffer,numlines*LINE_LENGTH*sizeof(short));
  1846.  
  1847.     close(write_fd);
  1848.  
  1849.     strcpy(filename,output_filename);
  1850.  
  1851.     changed=0;
  1852. }
  1853.  
  1854. void load_file()
  1855. {
  1856.     char input_filename[75]="";
  1857.  
  1858.     int retval;
  1859.  
  1860.     input_filename[68]=0;
  1861.  
  1862.     if (changed)
  1863.     {
  1864.         if (confirm_load()==0)
  1865.             return;
  1866.     }
  1867.  
  1868.     retval=get_inputline("Enter filename to load:",input_filename,input_filename);
  1869.  
  1870.     if (retval!=-1)
  1871.     {
  1872.         int retval;
  1873.  
  1874.         retval=do_load_file(input_filename);
  1875.  
  1876.         if (retval==0)
  1877.         {
  1878.             do_load_file("untitled.hwp");
  1879.  
  1880.             error_message("Unable to load:",input_filename);
  1881.         }
  1882.         else if (retval==-1)
  1883.         {
  1884.             error_message("Incorrect hwp version:",input_filename);
  1885.         }
  1886.     }
  1887. }
  1888.  
  1889. int confirm_quit()
  1890. {
  1891.     WINDOW *quit_window;
  1892.  
  1893.     int inputch;
  1894.  
  1895.     if (!changed)
  1896.         return 1;
  1897.  
  1898.     quit_window=newwin(5,70,10,5);
  1899.  
  1900.     mybox(quit_window,5,70);
  1901.  
  1902.     mvwaddstr(quit_window,1,1,"Are you sure you wish to quit?");
  1903.  
  1904.     mvwaddstr(quit_window,3,1,"Press Y to quit or N to return to the word processor.");
  1905.  
  1906.     wrefresh(quit_window);
  1907.  
  1908.     inputch=wgetch(quit_window);
  1909.  
  1910.     werase(quit_window);
  1911.  
  1912.     wrefresh(quit_window);
  1913.  
  1914.     delwin(quit_window);
  1915.  
  1916.     display_screen();
  1917.  
  1918.     switch(inputch)
  1919.     {
  1920.         case 'Y':
  1921.         case 'y':    return 1;
  1922.  
  1923.         default:    return 0;
  1924.     }
  1925. }
  1926.  
  1927. int options_menu()
  1928. {
  1929.     WINDOW *options_win;
  1930.  
  1931.     int inputch=0,pos=0,tmpint;
  1932.  
  1933.     char tmpstr[69];
  1934.  
  1935.     options_win=newwin(12,19,1,35);
  1936.  
  1937.     mybox(options_win,12,19);
  1938.  
  1939.     wattron(options_win,A_REVERSE);
  1940.  
  1941.     mvwaddstr(options_win,1,1,"Line Spacing");
  1942.  
  1943.     wattroff(options_win,A_REVERSE);
  1944.  
  1945.     mvwaddstr(options_win,2,1,"Page Length");
  1946.  
  1947.     mvwaddstr(options_win,3,1,"Tab Size");
  1948.  
  1949.     mvwaddstr(options_win,4,1,"Indentation Size");
  1950.  
  1951.     mvwaddstr(options_win,5,1,"Left Margin");
  1952.  
  1953.     mvwaddstr(options_win,6,1,"Right Margin");
  1954.  
  1955.     mvwaddstr(options_win,7,1,"Top Blanks");
  1956.  
  1957.     mvwaddstr(options_win,8,1,"Bottom Blanks");
  1958.  
  1959.     mvwaddstr(options_win,9,1,"Starting Page #");
  1960.  
  1961.     mvwaddstr(options_win,10,1,"Toggle Insertmode");
  1962.  
  1963.     keypad(options_win,TRUE);
  1964.  
  1965.     wmove(options_win,1,1);
  1966.  
  1967.     wrefresh(options_win);
  1968.  
  1969.     while(inputch!='\n'&&inputch!='\r'&&inputch!=27)
  1970.     {
  1971.         inputch=getch();
  1972.  
  1973.         switch(pos)
  1974.         {
  1975.             case 0:    mvwaddstr(options_win,1,1,"Line Spacing");
  1976.  
  1977.                 break;
  1978.  
  1979.             case 1:    mvwaddstr(options_win,2,1,"Page Length");
  1980.  
  1981.                 break;
  1982.  
  1983.             case 2:    mvwaddstr(options_win,3,1,"Tab Size");
  1984.  
  1985.                 break;
  1986.  
  1987.             case 3:    mvwaddstr(options_win,4,1,"Indentation Size");
  1988.  
  1989.                 break;
  1990.  
  1991.             case 4:    mvwaddstr(options_win,5,1,"Left Margin");
  1992.  
  1993.                 break;
  1994.  
  1995.             case 5:    mvwaddstr(options_win,6,1,"Right Margin");
  1996.  
  1997.                 break;
  1998.  
  1999.             case 6:    mvwaddstr(options_win,7,1,"Top Blanks");
  2000.  
  2001.                 break;
  2002.  
  2003.             case 7:    mvwaddstr(options_win,8,1,"Bottom Blanks");
  2004.  
  2005.                 break;
  2006.  
  2007.             case 8:    mvwaddstr(options_win,9,1,"Starting Page #");
  2008.  
  2009.                 break;
  2010.  
  2011.             case 9:    mvwaddstr(options_win,10,1,"Toggle Insertmode");
  2012.  
  2013.                 break;
  2014.         }
  2015.  
  2016.         switch(inputch)
  2017.         {
  2018.             case 12:    wrefresh(curscr);
  2019.  
  2020.                     break;
  2021.  
  2022.             case KEY_DOWN:    pos++;
  2023.  
  2024.                     break;
  2025.  
  2026.             case KEY_UP:    pos--;
  2027.  
  2028.                     if(pos<0)
  2029.                         pos+=10;
  2030.  
  2031.                     break;
  2032.  
  2033.             case KEY_RIGHT:    werase(options_win);
  2034.  
  2035.                     wrefresh(options_win);
  2036.  
  2037.                     delwin(options_win);
  2038.  
  2039.                     display_screen();
  2040.  
  2041.                     return -1;
  2042.  
  2043.             case KEY_LEFT:    werase(options_win);
  2044.  
  2045.                     wrefresh(options_win);
  2046.  
  2047.                     delwin(options_win);
  2048.  
  2049.                     display_screen();
  2050.  
  2051.                     return -2;
  2052.         }
  2053.  
  2054.         pos=pos%10;
  2055.  
  2056.         wattron(options_win,A_REVERSE);
  2057.  
  2058.         switch(pos)
  2059.         {
  2060.             case 0:    mvwaddstr(options_win,1,1,"Line Spacing");
  2061.                 wmove(options_win,1,1);
  2062.  
  2063.                 break;
  2064.  
  2065.             case 1:    mvwaddstr(options_win,2,1,"Page Length");
  2066.                 wmove(options_win,2,1);
  2067.  
  2068.                 break;
  2069.  
  2070.             case 2:    mvwaddstr(options_win,3,1,"Tab Size");
  2071.                 wmove(options_win,3,1);
  2072.  
  2073.                 break;
  2074.  
  2075.             case 3:    mvwaddstr(options_win,4,1,"Indentation Size");
  2076.                 wmove(options_win,4,1);
  2077.  
  2078.                 break;
  2079.  
  2080.             case 4:    mvwaddstr(options_win,5,1,"Left Margin");
  2081.                 wmove(options_win,5,1);
  2082.  
  2083.                 break;
  2084.  
  2085.             case 5:    mvwaddstr(options_win,6,1,"Right Margin");
  2086.                 wmove(options_win,6,1);
  2087.  
  2088.                 break;
  2089.  
  2090.             case 6:    mvwaddstr(options_win,7,1,"Top Blanks");
  2091.                 wmove(options_win,7,1);
  2092.  
  2093.                 break;
  2094.  
  2095.             case 7:    mvwaddstr(options_win,8,1,"Bottom Blanks");
  2096.                 wmove(options_win,8,1);
  2097.  
  2098.                 break;
  2099.  
  2100.             case 8:    mvwaddstr(options_win,9,1,"Starting Page #");
  2101.                 wmove(options_win,9,1);
  2102.  
  2103.                 break;
  2104.  
  2105.             case 9:    mvwaddstr(options_win,10,1,"Toggle Insertmode");
  2106.                 wmove(options_win,10,1);
  2107.  
  2108.                 break;
  2109.         }
  2110.  
  2111.         wattroff(options_win,A_REVERSE);
  2112.  
  2113.         wrefresh(options_win);
  2114.     }
  2115.  
  2116.     werase(options_win);
  2117.  
  2118.     wrefresh(options_win);
  2119.  
  2120.     delwin(options_win);
  2121.  
  2122.     display_screen();
  2123.  
  2124.     if (inputch==27)
  2125.         return 0;
  2126.  
  2127.     attron(A_REVERSE);
  2128.  
  2129.     mvaddstr(0,0,"                                                                        F1:Help ");
  2130.  
  2131.     attroff(A_REVERSE);
  2132.  
  2133.     refresh();
  2134.  
  2135.     switch(pos)
  2136.     {
  2137.         case 0:    sprintf(tmpstr,"%d",line_spacing+1);
  2138.  
  2139.             if (get_inputline("Change line spacing:",tmpstr,tmpstr)!=-1)
  2140.             {
  2141.                 sscanf(tmpstr,"%d",&tmpint);
  2142.  
  2143.                 if (tmpint>0&&tmpint<4)
  2144.                     line_spacing=tmpint-1;
  2145.                 else
  2146.                     error_message("Didn't change line spacing because:","Incorrect number entered - must be between 1 and 3.");
  2147.             }
  2148.  
  2149.             return 0;
  2150.  
  2151.         case 1:    sprintf(tmpstr,"%d",page_length);
  2152.  
  2153.             if (get_inputline("Change page length:",tmpstr,tmpstr)!=-1)
  2154.             {
  2155.                 sscanf(tmpstr,"%d",&tmpint);
  2156.  
  2157.                 if (tmpint>0&&tmpint<201)
  2158.                     page_length=tmpint;
  2159.                 else
  2160.                     error_message("Didn't change page length because:","Incorrect number entered - must be between 1 and 200.");
  2161.             }
  2162.  
  2163.             return 0;
  2164.  
  2165.         case 2: sprintf(tmpstr,"%d",tab_size);
  2166.  
  2167.             if (get_inputline("Change Tab size:",tmpstr,tmpstr)!=-1)
  2168.             {
  2169.                 sscanf(tmpstr,"%d",&tmpint);
  2170.  
  2171.                 if (tmpint>0&&tmpint<17)
  2172.                     tab_size=tmpint;
  2173.                 else
  2174.                     error_message("Didn't change Tab size because:","Incorrect number entered - must be between 1 and 16.");
  2175.             }
  2176.  
  2177.             return 0;
  2178.  
  2179.         case 3:    sprintf(tmpstr,"%d",indent+1);
  2180.  
  2181.             if (get_inputline("Change indentation size:",tmpstr,tmpstr)!=-1)
  2182.             {
  2183.                 sscanf(tmpstr,"%d",&tmpint);
  2184.  
  2185.                 tmpint--;
  2186.  
  2187.                 if (tmpint>=left_margin&&tmpint<=right_margin)
  2188.                     indent=tmpint;
  2189.                 else
  2190.                     error_message("Didn't change indentation size because:","Incorrect number entered - must be between left and right margin.");
  2191.             }
  2192.  
  2193.             return 0;
  2194.  
  2195.         case 4:    sprintf(tmpstr,"%d",left_margin+1);
  2196.  
  2197.             if (get_inputline("Change left margin:",tmpstr,tmpstr)!=-1)
  2198.             {
  2199.                 sscanf(tmpstr,"%d",&tmpint);
  2200.  
  2201.                 tmpint--;
  2202.  
  2203.                 if (tmpint<=indent&&tmpint<right_margin&&tmpint>=0)
  2204.                     left_margin=tmpint;
  2205.                 else
  2206.                     error_message("Didn't change left margin because:","Incorrect number entered - must be between indentation and right margin.");
  2207.             }
  2208.  
  2209.             return 0;
  2210.  
  2211.         case 5:    sprintf(tmpstr,"%d",right_margin+1);
  2212.  
  2213.             if (get_inputline("Change right margin:",tmpstr,tmpstr)!=-1)
  2214.             {
  2215.                 sscanf(tmpstr,"%d",&tmpint);
  2216.  
  2217.                 tmpint--;
  2218.  
  2219.                 if (tmpint>left_margin&&tmpint<=78)
  2220.                     right_margin=tmpint;
  2221.                 else
  2222.                     error_message("Didn't change right margin because:","Incorrect number entered - must be greater than left margin and less than 80.");
  2223.             }
  2224.  
  2225.             return 0;
  2226.  
  2227.         case 6:    sprintf(tmpstr,"%d",top_blanks);
  2228.  
  2229.             if (get_inputline("Change top blanks:",tmpstr,tmpstr)!=-1)
  2230.             {
  2231.                 sscanf(tmpstr,"%d",&tmpint);
  2232.  
  2233.                 if (tmpint>1&&tmpint<11)
  2234.                     top_blanks=tmpint;
  2235.                 else
  2236.                     error_message("Didn't change top blanks because:","Incorrect number entered - must be between 2 and 10.");
  2237.             }
  2238.  
  2239.             return 0;
  2240.  
  2241.         case 7:    sprintf(tmpstr,"%d",bottom_blanks);
  2242.  
  2243.             if (get_inputline("Change bottom blanks:",tmpstr,tmpstr)!=-1)
  2244.             {
  2245.                 sscanf(tmpstr,"%d",&tmpint);
  2246.  
  2247.                 if (tmpint>1&&tmpint<11)
  2248.                     bottom_blanks=tmpint;
  2249.                 else
  2250.                     error_message("Didn't change bottom blanks because:","Incorrect number entered - must be between 2 and 10.");
  2251.             }
  2252.  
  2253.             return 0;
  2254.  
  2255.         case 8:    sprintf(tmpstr,"%d",starting_page_no);
  2256.  
  2257.             if (get_inputline("Change starting page #:",tmpstr,tmpstr)!=-1)
  2258.             {
  2259.                 sscanf(tmpstr,"%d",&tmpint);
  2260.  
  2261.                 if (tmpint>0&&tmpint<201)
  2262.                     starting_page_no=tmpint;
  2263.                 else
  2264.                     error_message("Didn't change starting page # because:","Incorrect number entered - must be between 1 and 200.");
  2265.             }
  2266.  
  2267.             return 0;
  2268.  
  2269.         case 9:    insertmode=(insertmode==INSERT)?OVERSTRIKE:INSERT;
  2270.  
  2271.             mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  2272.  
  2273.             if (insertmode==OVERSTRIKE)
  2274.                 mvprintw(22,69,"Overstrike");
  2275.             else
  2276.                 mvprintw(22,69,"Insert    ");
  2277.  
  2278.             if (current_attr&START_BOLD)
  2279.                 mvprintw(22,64,"Bold");
  2280.             else
  2281.                 mvprintw(22,64,"    ");
  2282.         
  2283.             if (current_attr&START_UNDERLINE)
  2284.                 mvprintw(22,54,"Underline");
  2285.             else
  2286.                 mvprintw(22,54,"         ");
  2287.  
  2288.             return 0;
  2289.     }
  2290. }
  2291.  
  2292. int do_menubar()
  2293. {
  2294.     int pos=0,inputch=0,retval=0;
  2295.  
  2296.     attron(A_REVERSE);
  2297.  
  2298.     mvaddstr(0,0," F2:File    F3:Edit    F4:Locate    F5:Options    F6:Spell              F1:Help ");
  2299.  
  2300.     attroff(A_REVERSE);
  2301.  
  2302.     mvaddstr(0,1,"F2:File");
  2303.  
  2304.     move(0,1);
  2305.  
  2306.     refresh();
  2307.  
  2308.     while (inputch!=27&&retval==0)
  2309.     {
  2310.         inputch=getch();
  2311.  
  2312.         switch(inputch)
  2313.         {
  2314.             case 12:    wrefresh(curscr);
  2315.  
  2316.                     break;
  2317.  
  2318.             case KEY_RIGHT:    pos++;
  2319.  
  2320.                     pos=pos%6;
  2321.  
  2322.                     break;
  2323.  
  2324.             case KEY_LEFT:    pos--;
  2325.  
  2326.                     if (pos<0)
  2327.                         pos+=6;
  2328.  
  2329.                     break;
  2330.  
  2331.             case KEY_DOWN:
  2332.             case KEY_UP:
  2333.             case '\n':
  2334.             case '\r':    switch(pos)
  2335.                     {
  2336.                         case 0:    switch(file_menu())
  2337.                             {
  2338.                                 case 1:    retval=1;
  2339.  
  2340.                                     break;
  2341.  
  2342.                                 case -1:pos++;
  2343.  
  2344.                                     pos=pos%6;
  2345.  
  2346.                                     break;
  2347.  
  2348.                                 case -2:pos--;
  2349.  
  2350.                                     if (pos<0)
  2351.                                         pos+=6;
  2352.  
  2353.                                     break;
  2354.                             }
  2355.  
  2356.                             break;
  2357.  
  2358.                         case 1:    switch(edit_menu())
  2359.                             {
  2360.                                 case -1:pos++;
  2361.  
  2362.                                     pos=pos%6;
  2363.  
  2364.                                     break;
  2365.  
  2366.                                 case -2:pos--;
  2367.  
  2368.                                     if (pos<0)
  2369.                                         pos+=6;
  2370.  
  2371.                                     break;
  2372.                             }
  2373.  
  2374.                             break;
  2375.  
  2376.                         case 2:    switch(locate_menu())
  2377.                             {
  2378.                                 case -1:pos++;
  2379.  
  2380.                                     pos=pos%6;
  2381.  
  2382.                                     break;
  2383.  
  2384.                                 case -2:pos--;
  2385.  
  2386.                                     if (pos<0)
  2387.                                         pos+=6;
  2388.  
  2389.                                     break;
  2390.                             }
  2391.  
  2392.                             break;
  2393.  
  2394.  
  2395.                         case 3:    switch(options_menu())
  2396.                             {
  2397.                                 case -1:pos++;
  2398.  
  2399.                                     pos=pos%6;
  2400.  
  2401.                                     break;
  2402.  
  2403.                                 case -2:pos--;
  2404.  
  2405.                                     if (pos<0)
  2406.                                         pos+=6;
  2407.  
  2408.                                     break;
  2409.                             }
  2410.  
  2411.                             break;
  2412.  
  2413.                         case 4:    switch(spell_menu())
  2414.                             {
  2415.                                 case -1:pos++;
  2416.  
  2417.                                     pos=pos%6;
  2418.  
  2419.                                     break;
  2420.  
  2421.                                 case -2:pos--;
  2422.  
  2423.                                     if (pos<0)
  2424.                                         pos+=6;
  2425.  
  2426.                                     break;
  2427.                             }
  2428.  
  2429.                             break;
  2430.  
  2431.  
  2432.                         case 5:    do_help();
  2433.  
  2434.                             break;
  2435.                     }
  2436.         }
  2437.  
  2438.         attron(A_REVERSE);
  2439.  
  2440.         mvaddstr(0,0," F2:File    F3:Edit    F4:Locate    F5:Options    F6:Spell              F1:Help ");
  2441.  
  2442.         attroff(A_REVERSE);
  2443.  
  2444.         switch(pos)
  2445.         {
  2446.             case 0:    mvaddstr(0,1,"F2:File");
  2447.                 move(0,1);
  2448.  
  2449.                 break;
  2450.  
  2451.             case 1:    mvaddstr(0,12,"F3:Edit");
  2452.                 move(0,12);
  2453.  
  2454.                 break;
  2455.  
  2456.             case 2:    mvaddstr(0,23,"F4:Locate");
  2457.                 move(0,23);
  2458.  
  2459.                 break;
  2460.  
  2461.             case 3:    mvaddstr(0,36,"F5:Options");
  2462.                 move(0,36);
  2463.  
  2464.                 break;
  2465.  
  2466.             case 4:    mvaddstr(0,50,"F6:Spell");
  2467.                 move(0,50);
  2468.  
  2469.                 break;
  2470.  
  2471.             case 5:    mvaddstr(0,72,"F1:Help");
  2472.                 move(0,72);
  2473.  
  2474.                 break;
  2475.         }
  2476.  
  2477.         refresh();
  2478.     }
  2479.  
  2480.     attron(A_REVERSE);
  2481.  
  2482.     mvaddstr(0,0," F2:File    F3:Edit    F4:Locate    F5:Options    F6:Spell              F1:Help ");
  2483.  
  2484.     attroff(A_REVERSE);
  2485.  
  2486.     refresh();
  2487.  
  2488.     return retval;
  2489. }
  2490.  
  2491. int confirm_load()
  2492. {
  2493.     WINDOW *load_window;
  2494.  
  2495.     int inputch;
  2496.  
  2497.     if (!changed)
  2498.         return 1;
  2499.  
  2500.     load_window=newwin(5,70,10,5);
  2501.  
  2502.     mybox(load_window,5,70);
  2503.  
  2504.     mvwaddstr(load_window,1,1,"The file in the buffer has not yet been saved:");
  2505.  
  2506.     mvwaddstr(load_window,3,1,"Press Y to load a new file or N to return to the current file.");
  2507.  
  2508.     wrefresh(load_window);
  2509.  
  2510.     inputch=wgetch(load_window);
  2511.  
  2512.     werase(load_window);
  2513.  
  2514.     wrefresh(load_window);
  2515.  
  2516.     delwin(load_window);
  2517.  
  2518.     display_screen();
  2519.  
  2520.     switch(inputch)
  2521.     {
  2522.         case 'Y':
  2523.         case 'y':    return 1;
  2524.  
  2525.         default:    return 0;
  2526.     }
  2527. }
  2528.  
  2529. void justify(y_val)
  2530. int y_val;
  2531. {
  2532.     int start,pos,end,saveend,next=(1+line_spacing);
  2533.  
  2534.     if (y_val+next+((current_page-1)*LINES_PER_PAGE)>=numlines)
  2535.         return;
  2536.  
  2537.     for (start=left_margin;start<indent;start++)
  2538.     {
  2539.         if (file_buffer[start+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2540.                 file_buffer[start+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2541.         {
  2542.             break;
  2543.         }
  2544.     }
  2545.  
  2546.     if (start>=indent)
  2547.         return;
  2548.  
  2549.     for (pos=right_margin;pos>left_margin;pos--)
  2550.         if (file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2551.                 file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2552.             break;
  2553.  
  2554.     for (end=start;end<=right_margin;end++)
  2555.         if (file_buffer[end+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]==0||
  2556.                 file_buffer[end+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]==' ')
  2557.             break;
  2558.  
  2559.     end--;
  2560.  
  2561.     if (file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2562.             file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2563.         pos+=2;
  2564.  
  2565.     if ((pos+end-start)>right_margin)
  2566.         return;
  2567.  
  2568.     saveend=end;
  2569.  
  2570.     for (;end>=start;end--)
  2571.         file_buffer[pos+end-start+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=
  2572.                 file_buffer[end+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  2573.  
  2574.     for (end=saveend+1;end<=right_margin;end++)
  2575.         if (file_buffer[end+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2576.                 file_buffer[end+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2577.             break;
  2578.  
  2579.     for (pos=left_margin;pos<=(right_margin-(end-start));pos++)
  2580.         file_buffer[pos+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=
  2581.                 file_buffer[pos+end-start+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  2582.  
  2583.     for (;pos<=right_margin;pos++)
  2584.         file_buffer[pos+(y_val+next+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=0;
  2585.         
  2586.     justify(y_val);
  2587.  
  2588.     justify(y_val+next);
  2589.  
  2590.     for (pos=0;pos<(LINE_LENGTH*(1+line_spacing));pos++)
  2591.         if (file_buffer[pos+(numlines-1-(1+line_spacing))*LINE_LENGTH]!=0&&
  2592.             file_buffer[pos+(numlines-1-(1+line_spacing))*LINE_LENGTH]!=' ')
  2593.             break;
  2594.  
  2595.     if (pos>=(LINE_LENGTH*(1+line_spacing)))
  2596.     {
  2597.         numlines-=(1+line_spacing);
  2598.     
  2599.         file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  2600.                     
  2601.         if (file_buffer==NULL)
  2602.             my_exit("Realloc error on file buffer.\n");
  2603.     }
  2604. }
  2605.  
  2606. void insert(str,len,y_val)
  2607. short str[LINE_LENGTH];
  2608. int len;
  2609. {
  2610.     int pos,pos1=0,saved_pos=0,start,next_line=0,offset=0;
  2611.  
  2612.     short tmpstr[LINE_LENGTH];
  2613.  
  2614.     memset(tmpstr,0,((unsigned)LINE_LENGTH*sizeof(short)));
  2615.  
  2616.     for (pos=right_margin-len;pos<=right_margin;pos++)
  2617.         if (file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2618.                 file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2619.             break;
  2620.  
  2621.     if (current_x==right_margin&&pos>right_margin)
  2622.         pos=right_margin;
  2623.  
  2624.     if (pos==right_margin-len)
  2625.     {
  2626.         for (;pos>=left_margin;pos--)
  2627.             if (file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]==0||
  2628.                     file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]==' ')
  2629.                 break;
  2630.  
  2631.         if (pos<=left_margin)
  2632.         {
  2633.             beep();
  2634.  
  2635.             return;
  2636.         }
  2637.  
  2638.         pos++;
  2639.     }
  2640.  
  2641.     if (pos<=current_x)
  2642.         next_line++;
  2643.  
  2644.     if (pos<=right_margin)
  2645.     {
  2646.         for (pos1=right_margin;pos1>=pos;pos1--)
  2647.             if (file_buffer[pos1+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2648.                     file_buffer[pos1+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2649.                 break;
  2650.  
  2651.         pos1+=2;
  2652.  
  2653.         saved_pos=pos;
  2654.  
  2655.         for (;pos<=pos1;pos++)
  2656.         {
  2657.             if (next_line&&pos>=current_x)
  2658.             {
  2659.                 if (!offset)
  2660.                     offset=pos1-pos;
  2661.  
  2662.                 tmpstr[pos-saved_pos+len]=file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  2663.             }
  2664.             else
  2665.                 tmpstr[pos-saved_pos]=file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  2666.  
  2667.             file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=0;
  2668.         }
  2669.  
  2670.         if (next_line)
  2671.         {
  2672.             pos1+=len;
  2673.  
  2674.             for (pos=current_x;pos<current_x+len;pos++)
  2675.                 tmpstr[pos-saved_pos]=str[pos-current_x];
  2676.         }
  2677.  
  2678.         tmpstr[pos++]=' ';
  2679.  
  2680.         saved_pos=pos1-saved_pos;
  2681.     }
  2682.     
  2683.     for (pos=right_margin;pos>=(current_x+len);pos--)
  2684.         file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=
  2685.                 file_buffer[pos-len+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH];
  2686.  
  2687.     if (!next_line)
  2688.     {
  2689.         for (pos=current_x;pos<current_x+len;pos++)
  2690.             file_buffer[pos+(y_val+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]=
  2691.                     str[pos-current_x];
  2692.     }
  2693.  
  2694.     if (saved_pos>0)
  2695.     {
  2696.         int save_x=current_x;
  2697.  
  2698.         if (y_val+(1+line_spacing)+((current_page-1)*LINES_PER_PAGE)>=numlines)
  2699.         {
  2700.             numlines+=(1+line_spacing);
  2701.     
  2702.             file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  2703.                     
  2704.             if (file_buffer==NULL)
  2705.                 my_exit("Realloc error on file buffer.\n");
  2706.     
  2707.             memset(file_buffer+(numlines-(1+line_spacing))*LINE_LENGTH,0,((unsigned)(1+line_spacing)*LINE_LENGTH*sizeof(short)));
  2708.         }
  2709.         else
  2710.         {
  2711.             for (start=left_margin;start<indent;start++)
  2712.             {
  2713.                 if (file_buffer[start+(y_val+(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=0&&
  2714.                         file_buffer[start+(y_val+(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH]!=' ')
  2715.                 {
  2716.                     break;
  2717.                 }
  2718.             }
  2719.  
  2720.             if (start>=indent)
  2721.             {
  2722.                 numlines+=(1+line_spacing);
  2723.         
  2724.                 file_buffer=(short *) realloc(file_buffer,numlines*LINE_LENGTH*sizeof(short));
  2725.                         
  2726.                 if (file_buffer==NULL)
  2727.                     my_exit("Realloc error on file buffer.\n");
  2728.         
  2729.                 memset(file_buffer+(numlines-(1+line_spacing))*LINE_LENGTH,0,((unsigned)(1+line_spacing)*LINE_LENGTH*sizeof(short)));
  2730.         
  2731.                 if ((y_val+(current_page-1)*LINES_PER_PAGE+(1+line_spacing))<(numlines-(1+line_spacing)))
  2732.                 {
  2733.                     int line;
  2734.  
  2735.                     for (line=numlines-1;line>=y_val+2*(1+line_spacing)+(current_page-1)*LINES_PER_PAGE;line--)
  2736.                         memcpy(file_buffer+line*LINE_LENGTH,file_buffer+(line-(1+line_spacing))*LINE_LENGTH,LINE_LENGTH*sizeof(short));
  2737.  
  2738.                     memset(file_buffer+(y_val+(1+line_spacing)+(current_page-1)*LINES_PER_PAGE)*LINE_LENGTH,0,((unsigned)80*sizeof(short)));
  2739.                 }
  2740.             }
  2741.         }
  2742.  
  2743.         current_x=left_margin;
  2744.  
  2745.         insert(tmpstr,saved_pos,y_val+(1+line_spacing));
  2746.  
  2747.         if (next_line)
  2748.             current_x=-(left_margin+saved_pos-(offset));
  2749.         else
  2750.             current_x=save_x+len;
  2751.     }
  2752.     else
  2753.     {
  2754.         current_x+=len;
  2755.     }
  2756. }
  2757.  
  2758. void print_file()
  2759. {
  2760.     char prn_file[65],tempstr1[LINE_LENGTH+1],tempstr2[LINE_LENGTH+1];
  2761.  
  2762.     FILE *prn_fd;
  2763.  
  2764.     int line=0,page=starting_page_no,counter,bold_on=0,underline_on=0,pos,footnote_on=0;
  2765.  
  2766.     sprintf(prn_file,"/tmp/hwpprn.%d",getpid());
  2767.  
  2768.     errno=0;
  2769.  
  2770.     prn_fd=fopen(prn_file,"w");
  2771.  
  2772.     if (prn_fd==NULL)
  2773.     {
  2774.         error_message("Couldn't open temporary print file because:",strerror(errno));
  2775.  
  2776.         return;
  2777.     }
  2778.  
  2779.     fprintf(prn_fd,"%c%c",27,'n');
  2780.  
  2781.     while(line<numlines)
  2782.     {
  2783.         memset(tempstr1,0,((unsigned)LINE_LENGTH));
  2784.  
  2785.         tempstr1[LINE_LENGTH]=0;
  2786.  
  2787.         if (strcspn(header,"%"))
  2788.             strncpy(tempstr1,header,strcspn(header,"%"));
  2789.  
  2790.         if (strchr(header,'%'))
  2791.         {
  2792.             sprintf(tempstr2,"%d",page);
  2793.  
  2794.             strcat(tempstr1,tempstr2);
  2795.  
  2796.             strcat(tempstr1,strchr(header,'%')+1);
  2797.         }
  2798.  
  2799.         memset(tempstr2,' ',((unsigned)LINE_LENGTH));
  2800.  
  2801.         tempstr2[LINE_LENGTH]=0;
  2802.  
  2803.         strcpy(tempstr2+LINE_LENGTH-strlen(tempstr1),tempstr1);
  2804.         strcat(tempstr2,"\n");
  2805.  
  2806.         if (top_blanks<3)
  2807.         {
  2808.             fputs(tempstr2,prn_fd);
  2809.  
  2810.             for (counter=1;counter<top_blanks;counter++)
  2811.                 fputc('\n',prn_fd);
  2812.         }
  2813.         else
  2814.         {
  2815.             fputc('\n',prn_fd);
  2816.  
  2817.             fputs(tempstr2,prn_fd);
  2818.  
  2819.             for (counter=2;counter<top_blanks;counter++)
  2820.                 fputc('\n',prn_fd);
  2821.         }
  2822.  
  2823.         for (counter=0;counter<page_length-top_blanks-bottom_blanks&&line<numlines;counter++)
  2824.         {
  2825.             for (pos=0;pos<LINE_LENGTH;pos++)
  2826.             {
  2827.                 if (file_buffer[pos+line*LINE_LENGTH]&START_UNDERLINE)
  2828.                 {
  2829.                     underline_on++;
  2830.  
  2831.                     fprintf(prn_fd,"%c%c%c",27,'-',1);
  2832.                 }
  2833.  
  2834.                 if (file_buffer[pos+line*LINE_LENGTH]&END_UNDERLINE)
  2835.                 {
  2836.                     underline_on=0;
  2837.  
  2838.                     fprintf(prn_fd,"%c%c%c",27,'-',0);
  2839.                 }
  2840.  
  2841.                 if (file_buffer[pos+line*LINE_LENGTH]&START_BOLD)
  2842.                 {
  2843.                     bold_on++;
  2844.  
  2845.                     fprintf(prn_fd,"%c%c",27,'E');
  2846.                 }
  2847.  
  2848.                 if (file_buffer[pos+line*LINE_LENGTH]&END_BOLD)
  2849.                 {
  2850.                     bold_on=0;
  2851.  
  2852.                     fprintf(prn_fd,"%c%c",27,'F');
  2853.                 }
  2854.  
  2855.                 if (file_buffer[pos+line*LINE_LENGTH]&START_FOOTNOTE)
  2856.                 {
  2857.                     footnote_on++;
  2858.  
  2859.                     fprintf(prn_fd,"%c%c%c%c%c",27,'P',27,'S',0);
  2860.                 }
  2861.  
  2862.                 if (file_buffer[pos+line*LINE_LENGTH]&END_FOOTNOTE)
  2863.                 {
  2864.                     footnote_on=0;
  2865.  
  2866.                     fprintf(prn_fd,"%c%c%c%c",27,'n',27,'T');
  2867.                 }
  2868.  
  2869.                 if (file_buffer[pos+line*LINE_LENGTH])
  2870.                     fputc(file_buffer[pos+line*LINE_LENGTH],prn_fd);
  2871.                 else
  2872.                     fputc(' ',prn_fd);
  2873.             }
  2874.  
  2875.             fputc('\n',prn_fd);
  2876.  
  2877.             if (underline_on)
  2878.             {
  2879.                 underline_on=0;
  2880.  
  2881.                 fprintf(prn_fd,"%c%c%c",27,'-',0);
  2882.             }
  2883.  
  2884.             if (bold_on)
  2885.             {
  2886.                 bold_on=0;
  2887.  
  2888.                 fprintf(prn_fd,"%c%c",27,'F');
  2889.             }
  2890.  
  2891.             if (footnote_on)
  2892.             {
  2893.                 footnote_on=0;
  2894.  
  2895.                 fprintf(prn_fd,"%c%c%c%c",27,'n',27,'T');
  2896.             }
  2897.  
  2898.             line++;
  2899.         }
  2900.  
  2901.         if (line>=numlines)
  2902.             for (;counter<page_length-top_blanks-bottom_blanks;counter++)
  2903.                 fputc('\n',prn_fd);
  2904.  
  2905.         memset(tempstr1,0,((unsigned)LINE_LENGTH));
  2906.  
  2907.         tempstr1[LINE_LENGTH]=0;
  2908.  
  2909.         if (strcspn(footer,"%"))
  2910.             strncpy(tempstr1,footer,strcspn(footer,"%"));
  2911.  
  2912.         if (strchr(footer,'%'))
  2913.         {
  2914.             sprintf(tempstr2,"%d",page);
  2915.  
  2916.             strcat(tempstr1,tempstr2);
  2917.  
  2918.             strcat(tempstr1,strchr(footer,'%')+1);
  2919.         }
  2920.  
  2921.         memset(tempstr2,' ',((unsigned)LINE_LENGTH));
  2922.  
  2923.         tempstr2[LINE_LENGTH]=0;
  2924.  
  2925.         strcpy(tempstr2+LINE_LENGTH-strlen(tempstr1),tempstr1);
  2926.         strcat(tempstr2,"\n");
  2927.  
  2928.         if (bottom_blanks<3)
  2929.         {
  2930.             for (counter=1;counter<bottom_blanks;counter++)
  2931.                 fputc('\n',prn_fd);
  2932.  
  2933.             fputs(tempstr2,prn_fd);
  2934.         }
  2935.         else
  2936.         {
  2937.             for (counter=2;counter<bottom_blanks;counter++)
  2938.                 fputc('\n',prn_fd);
  2939.  
  2940.             fputs(tempstr2,prn_fd);
  2941.  
  2942.             fputc('\n',prn_fd);
  2943.         }
  2944.  
  2945.         fflush(prn_fd);
  2946.  
  2947.         page++;
  2948.     }
  2949.  
  2950.     close(prn_fd);
  2951.  
  2952.     sprintf(tempstr1,"%s %s",SPOOLER,prn_file);
  2953.  
  2954.     system(tempstr1);
  2955.  
  2956.     unlink(prn_file);
  2957.  
  2958.     clearok(mystdscr,TRUE);
  2959.  
  2960.     refresh();
  2961.  
  2962.     clearok(textscr,TRUE);
  2963.  
  2964.     wrefresh(textscr);
  2965. }
  2966.  
  2967. int edit_menu()
  2968. {
  2969.     WINDOW *edit_win;
  2970.  
  2971.     int inputch=0,pos=0;
  2972.  
  2973.     edit_win=newwin(10,17,1,11);
  2974.  
  2975.     mybox(edit_win,10,17);
  2976.  
  2977.     wattron(edit_win,A_REVERSE);
  2978.  
  2979.     mvwaddstr(edit_win,1,1,"Edit Header");
  2980.  
  2981.     wattroff(edit_win,A_REVERSE);
  2982.  
  2983.     mvwaddstr(edit_win,2,1,"Edit Footer");
  2984.  
  2985.     mvwaddstr(edit_win,3,1,"Start Underline");
  2986.  
  2987.     mvwaddstr(edit_win,4,1,"End Underline");
  2988.  
  2989.     mvwaddstr(edit_win,5,1,"Start Bold");
  2990.  
  2991.     mvwaddstr(edit_win,6,1,"End Bold");
  2992.  
  2993.     mvwaddstr(edit_win,7,1,"Start Footnote");
  2994.  
  2995.     mvwaddstr(edit_win,8,1,"End Footnote");
  2996.  
  2997.     keypad(edit_win,TRUE);
  2998.  
  2999.     wmove(edit_win,1,1);
  3000.  
  3001.     wrefresh(edit_win);
  3002.  
  3003.     while(inputch!='\n'&&inputch!='\r'&&inputch!=27)
  3004.     {
  3005.         inputch=getch();
  3006.  
  3007.         switch(pos)
  3008.         {
  3009.             case 0:    mvwaddstr(edit_win,1,1,"Edit Header");
  3010.  
  3011.                 break;
  3012.  
  3013.             case 1:    mvwaddstr(edit_win,2,1,"Edit Footer");
  3014.  
  3015.                 break;
  3016.  
  3017.             case 2:    mvwaddstr(edit_win,3,1,"Start Underline");
  3018.  
  3019.                 break;
  3020.  
  3021.             case 3:    mvwaddstr(edit_win,4,1,"End Underline");
  3022.  
  3023.                 break;
  3024.  
  3025.             case 4:    mvwaddstr(edit_win,5,1,"Start Bold");
  3026.  
  3027.                 break;
  3028.  
  3029.             case 5:    mvwaddstr(edit_win,6,1,"End Bold");
  3030.  
  3031.                 break;
  3032.  
  3033.             case 6: mvwaddstr(edit_win,7,1,"Start Footnote");
  3034.  
  3035.                 break;
  3036.  
  3037.             case 7: mvwaddstr(edit_win,8,1,"End Footnote");
  3038.  
  3039.                 break;
  3040.         }
  3041.  
  3042.         switch(inputch)
  3043.         {
  3044.             case 12:    wrefresh(curscr);
  3045.  
  3046.                     break;
  3047.  
  3048.             case KEY_DOWN:    pos++;
  3049.  
  3050.                     break;
  3051.  
  3052.             case KEY_UP:    pos--;
  3053.  
  3054.                     if(pos<0)
  3055.                         pos+=8;
  3056.  
  3057.                     break;
  3058.  
  3059.             case KEY_RIGHT:    werase(edit_win);
  3060.  
  3061.                     wrefresh(edit_win);
  3062.  
  3063.                     delwin(edit_win);
  3064.  
  3065.                     display_screen();
  3066.  
  3067.                     return -1;
  3068.  
  3069.             case KEY_LEFT:    werase(edit_win);
  3070.  
  3071.                     wrefresh(edit_win);
  3072.  
  3073.                     delwin(edit_win);
  3074.  
  3075.                     display_screen();
  3076.  
  3077.                     return -2;
  3078.         }
  3079.  
  3080.         pos=pos%8;
  3081.  
  3082.         wattron(edit_win,A_REVERSE);
  3083.  
  3084.         switch(pos)
  3085.         {
  3086.             case 0:    mvwaddstr(edit_win,1,1,"Edit Header");
  3087.                 wmove(edit_win,1,1);
  3088.  
  3089.                 break;
  3090.  
  3091.             case 1:    mvwaddstr(edit_win,2,1,"Edit Footer");
  3092.                 wmove(edit_win,2,1);
  3093.  
  3094.                 break;
  3095.  
  3096.             case 2:    mvwaddstr(edit_win,3,1,"Start Underline");
  3097.                 wmove(edit_win,3,1);
  3098.  
  3099.                 break;
  3100.  
  3101.             case 3:    mvwaddstr(edit_win,4,1,"End Underline");
  3102.                 wmove(edit_win,4,1);
  3103.  
  3104.                 break;
  3105.  
  3106.             case 4:    mvwaddstr(edit_win,5,1,"Start Bold");
  3107.                 wmove(edit_win,5,1);
  3108.  
  3109.                 break;
  3110.  
  3111.             case 5: mvwaddstr(edit_win,6,1,"End Bold");
  3112.                 wmove(edit_win,6,1);
  3113.  
  3114.                 break;
  3115.  
  3116.             case 6: mvwaddstr(edit_win,7,1,"Start Footnote");
  3117.                 wmove(edit_win,7,1);
  3118.  
  3119.                 break;
  3120.  
  3121.             case 7: mvwaddstr(edit_win,8,1,"End Footnote");
  3122.                 wmove(edit_win,8,1);
  3123.  
  3124.                 break;
  3125.         }
  3126.  
  3127.         wattroff(edit_win,A_REVERSE);
  3128.  
  3129.         wrefresh(edit_win);
  3130.     }
  3131.  
  3132.     werase(edit_win);
  3133.  
  3134.     wrefresh(edit_win);
  3135.  
  3136.     delwin(edit_win);
  3137.  
  3138.     display_screen();
  3139.  
  3140.     if (inputch==27)
  3141.         return 0;
  3142.  
  3143.     attron(A_REVERSE);
  3144.  
  3145.     mvaddstr(0,0,"                                                                        F1:Help ");
  3146.  
  3147.     attroff(A_REVERSE);
  3148.  
  3149.     refresh();
  3150.  
  3151.     switch(pos)
  3152.     {
  3153.         case 0:    get_inputline("Change header (first % will be page no.):",header,header);
  3154.  
  3155.             return 0;
  3156.  
  3157.         case 1:    get_inputline("Change footer (first % will be page no.):",footer,footer);
  3158.  
  3159.             return 0;
  3160.  
  3161.         case 2:    current_attr|=START_UNDERLINE;
  3162.  
  3163.             mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  3164.  
  3165.             if (insertmode==OVERSTRIKE)
  3166.                 mvprintw(22,69,"Overstrike");
  3167.             else
  3168.                 mvprintw(22,69,"Insert    ");
  3169.  
  3170.             if (current_attr&START_BOLD)
  3171.                 mvprintw(22,64,"Bold");
  3172.             else
  3173.                 mvprintw(22,64,"    ");
  3174.         
  3175.             if (current_attr&START_UNDERLINE)
  3176.                 mvprintw(22,54,"Underline");
  3177.             else
  3178.                 mvprintw(22,54,"         ");
  3179.  
  3180.             return 0;
  3181.  
  3182.         case 3:    current_attr|=END_UNDERLINE;
  3183.  
  3184.             mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  3185.  
  3186.             if (insertmode==OVERSTRIKE)
  3187.                 mvprintw(22,69,"Overstrike");
  3188.             else
  3189.                 mvprintw(22,69,"Insert    ");
  3190.  
  3191.             if (current_attr&START_BOLD)
  3192.                 mvprintw(22,64,"Bold");
  3193.             else
  3194.                 mvprintw(22,64,"    ");
  3195.         
  3196.             if (current_attr&START_UNDERLINE)
  3197.                 mvprintw(22,54,"Underline");
  3198.             else
  3199.                 mvprintw(22,54,"         ");
  3200.  
  3201.             return 0;
  3202.  
  3203.         case 4:    current_attr|=START_BOLD;
  3204.  
  3205.             mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  3206.  
  3207.             if (insertmode==OVERSTRIKE)
  3208.                 mvprintw(22,69,"Overstrike");
  3209.             else
  3210.                 mvprintw(22,69,"Insert    ");
  3211.  
  3212.             if (current_attr&START_BOLD)
  3213.                 mvprintw(22,64,"Bold");
  3214.             else
  3215.                 mvprintw(22,64,"    ");
  3216.         
  3217.             if (current_attr&START_UNDERLINE)
  3218.                 mvprintw(22,54,"Underline");
  3219.             else
  3220.                 mvprintw(22,54,"         ");
  3221.  
  3222.             return 0;
  3223.  
  3224.         case 5:    current_attr|=END_BOLD;
  3225.  
  3226.             mvprintw(22,0," Title: %s Page: %d Line: %d     ",(strrchr(filename,'/'))?strrchr(filename,'/')+1:filename,((current_page-1)*LINES_PER_PAGE+current_y)/(page_length-(top_blanks+bottom_blanks))+1,current_y+1+(current_page-1)*LINES_PER_PAGE);
  3227.  
  3228.             if (insertmode==OVERSTRIKE)
  3229.                 mvprintw(22,69,"Overstrike");
  3230.             else
  3231.                 mvprintw(22,69,"Insert    ");
  3232.  
  3233.             if (current_attr&START_BOLD)
  3234.                 mvprintw(22,64,"Bold");
  3235.             else
  3236.                 mvprintw(22,64,"    ");
  3237.         
  3238.             if (current_attr&START_UNDERLINE)
  3239.                 mvprintw(22,54,"Underline");
  3240.             else
  3241.                 mvprintw(22,54,"         ");
  3242.  
  3243.             return 0;
  3244.  
  3245.         case 6:    current_attr|=START_FOOTNOTE;
  3246.  
  3247.             return 0;
  3248.  
  3249.         case 7: current_attr|=END_FOOTNOTE;
  3250.  
  3251.             return 0;
  3252.     }
  3253. }
  3254.  
  3255. void write_ascii(ascii_file)
  3256. char *ascii_file;
  3257. {
  3258.     char tempstr1[LINE_LENGTH+1],tempstr2[LINE_LENGTH+1];
  3259.  
  3260.     FILE *ascii_fd;
  3261.  
  3262.     int line=0,page=starting_page_no,counter,bold_on=0,underline_on=0,pos;
  3263.  
  3264.     errno=0;
  3265.  
  3266.     ascii_fd=fopen(ascii_file,"w");
  3267.  
  3268.     if (ascii_fd==NULL)
  3269.     {
  3270.         error_message("Couldn't open ASCII file because:",strerror(errno));
  3271.  
  3272.         return;
  3273.     }
  3274.  
  3275.     while(line<numlines)
  3276.     {
  3277.         memset(tempstr1,0,((unsigned)LINE_LENGTH));
  3278.  
  3279.         tempstr1[LINE_LENGTH]=0;
  3280.  
  3281.         if (strcspn(header,"%"))
  3282.             strncpy(tempstr1,header,strcspn(header,"%"));
  3283.  
  3284.         if (strchr(header,'%'))
  3285.         {
  3286.             sprintf(tempstr2,"%d",page);
  3287.  
  3288.             strcat(tempstr1,tempstr2);
  3289.  
  3290.             strcat(tempstr1,strchr(header,'%')+1);
  3291.         }
  3292.  
  3293.         memset(tempstr2,' ',((unsigned)LINE_LENGTH));
  3294.  
  3295.         tempstr2[LINE_LENGTH]=0;
  3296.  
  3297.         strcpy(tempstr2+LINE_LENGTH-strlen(tempstr1),tempstr1);
  3298.         strcat(tempstr2,"\n");
  3299.  
  3300.         fputs(tempstr2,ascii_fd);
  3301.  
  3302.         for (counter=1;counter<top_blanks;counter++)
  3303.             fputc('\n',ascii_fd);
  3304.  
  3305.         for (counter=0;counter<page_length-top_blanks-bottom_blanks&&line<numlines;counter++)
  3306.         {
  3307.             for (pos=0;pos<LINE_LENGTH;pos++)
  3308.             {
  3309.                 if (file_buffer[pos+line*LINE_LENGTH])
  3310.                     fputc(file_buffer[pos+line*LINE_LENGTH],ascii_fd);
  3311.                 else
  3312.                     fputc(' ',ascii_fd);
  3313.             }
  3314.  
  3315.             fputc('\n',ascii_fd);
  3316.  
  3317.             line++;
  3318.         }
  3319.  
  3320.         if (line>=numlines)
  3321.             for (;counter<page_length-top_blanks-bottom_blanks;counter++)
  3322.                 fputc('\n',ascii_fd);
  3323.  
  3324.         for (counter=1;counter<bottom_blanks;counter++)
  3325.             fputc('\n',ascii_fd);
  3326.  
  3327.         memset(tempstr1,0,((unsigned)LINE_LENGTH));
  3328.  
  3329.         tempstr1[LINE_LENGTH]=0;
  3330.  
  3331.         if (strcspn(footer,"%"))
  3332.             strncpy(tempstr1,footer,strcspn(footer,"%"));
  3333.  
  3334.         if (strchr(footer,'%'))
  3335.         {
  3336.             sprintf(tempstr2,"%d",page);
  3337.  
  3338.             strcat(tempstr1,tempstr2);
  3339.  
  3340.             strcat(tempstr1,strchr(footer,'%')+1);
  3341.         }
  3342.  
  3343.         memset(tempstr2,' ',((unsigned)LINE_LENGTH));
  3344.  
  3345.         tempstr2[LINE_LENGTH]=0;
  3346.  
  3347.         strcpy(tempstr2+LINE_LENGTH-strlen(tempstr1),tempstr1);
  3348.         strcat(tempstr2,"\n");
  3349.  
  3350.         fputs(tempstr2,ascii_fd);
  3351.  
  3352.         fflush(ascii_fd);
  3353.  
  3354.         page++;
  3355.     }
  3356.  
  3357.     close(ascii_fd);
  3358. }
  3359.  
  3360. int locate_menu()
  3361. {
  3362.     WINDOW *locate_win;
  3363.  
  3364.     int inputch=0,pos=0;
  3365.  
  3366.     static find_type=0;
  3367.  
  3368.     static char search_str[LINE_LENGTH+1]="";
  3369.  
  3370.     locate_win=newwin(5,25,1,23);
  3371.  
  3372.     mybox(locate_win,5,25);
  3373.  
  3374.     wattron(locate_win,A_REVERSE);
  3375.  
  3376.     mvwaddstr(locate_win,1,1,"Find (case sensitive)");
  3377.  
  3378.     wattroff(locate_win,A_REVERSE);
  3379.  
  3380.     mvwaddstr(locate_win,2,1,"Find (case insensitive)");
  3381.  
  3382.     mvwaddstr(locate_win,3,1,"Find next");
  3383.  
  3384.     keypad(locate_win,TRUE);
  3385.  
  3386.     wmove(locate_win,1,1);
  3387.  
  3388.     wrefresh(locate_win);
  3389.  
  3390.     while(inputch!='\n'&&inputch!='\r'&&inputch!=27)
  3391.     {
  3392.         inputch=getch();
  3393.  
  3394.         switch(pos)
  3395.         {
  3396.             case 0:    mvwaddstr(locate_win,1,1,"Find (case sensitive)");
  3397.  
  3398.                 break;
  3399.  
  3400.             case 1:    mvwaddstr(locate_win,2,1,"Find (case insensitive)");
  3401.  
  3402.                 break;
  3403.  
  3404.             case 2:    mvwaddstr(locate_win,3,1,"Find next");
  3405.  
  3406.                 break;
  3407.         }
  3408.  
  3409.         switch(inputch)
  3410.         {
  3411.             case 12:    wrefresh(curscr);
  3412.  
  3413.                     break;
  3414.  
  3415.             case KEY_DOWN:    pos++;
  3416.  
  3417.                     break;
  3418.  
  3419.             case KEY_UP:    pos--;
  3420.  
  3421.                     if(pos<0)
  3422.                         pos+=3;
  3423.  
  3424.                     break;
  3425.  
  3426.             case KEY_RIGHT:    werase(locate_win);
  3427.  
  3428.                     wrefresh(locate_win);
  3429.  
  3430.                     delwin(locate_win);
  3431.  
  3432.                     display_screen();
  3433.  
  3434.                     return -1;
  3435.  
  3436.             case KEY_LEFT:    werase(locate_win);
  3437.  
  3438.                     wrefresh(locate_win);
  3439.  
  3440.                     delwin(locate_win);
  3441.  
  3442.                     display_screen();
  3443.  
  3444.                     return -2;
  3445.         }
  3446.  
  3447.         pos=pos%3;
  3448.  
  3449.         wattron(locate_win,A_REVERSE);
  3450.  
  3451.         switch(pos)
  3452.         {
  3453.             case 0:    mvwaddstr(locate_win,1,1,"Find (case sensitive)");
  3454.                 wmove(locate_win,1,1);
  3455.  
  3456.                 break;
  3457.  
  3458.             case 1:    mvwaddstr(locate_win,2,1,"Find (case insensitive)");
  3459.                 wmove(locate_win,2,1);
  3460.  
  3461.                 break;
  3462.  
  3463.             case 2:    mvwaddstr(locate_win,3,1,"Find next");
  3464.                 wmove(locate_win,3,1);
  3465.  
  3466.                 break;
  3467.         }
  3468.  
  3469.         wattroff(locate_win,A_REVERSE);
  3470.  
  3471.         wrefresh(locate_win);
  3472.     }
  3473.  
  3474.     werase(locate_win);
  3475.  
  3476.     wrefresh(locate_win);
  3477.  
  3478.     delwin(locate_win);
  3479.  
  3480.     display_screen();
  3481.  
  3482.     if (inputch==27)
  3483.         return 0;
  3484.  
  3485.     attron(A_REVERSE);
  3486.  
  3487.     mvaddstr(0,0,"                                                                        F1:Help ");
  3488.  
  3489.     attroff(A_REVERSE);
  3490.  
  3491.     refresh();
  3492.  
  3493.     switch(pos)
  3494.     {
  3495.         case 0:    if (get_inputline("Enter search string:",search_str,search_str)==-1)
  3496.                 return 0;
  3497.  
  3498.             find_type=CASE;
  3499.  
  3500.             do_search(search_str,find_type,1);
  3501.  
  3502.             display_screen();
  3503.  
  3504.             return 0;
  3505.  
  3506.         case 1:    if (get_inputline("Enter search string:",search_str,search_str)==-1)
  3507.                 return 0;
  3508.  
  3509.             find_type=NOCASE;
  3510.  
  3511.             do_search(search_str,find_type,1);
  3512.  
  3513.             display_screen();
  3514.  
  3515.             return 0;
  3516.  
  3517.         case 2:    do_search(search_str,find_type,0);
  3518.  
  3519.             display_screen();
  3520.  
  3521.             return 0;
  3522.     }
  3523. }
  3524.  
  3525. int do_search(string,case_type,first)
  3526. char *string;
  3527. int case_type,first;
  3528. {
  3529.     int line,pos,cont,goodpos,len;
  3530.  
  3531.     len=strlen(string);
  3532.  
  3533.     if (case_type==NOCASE)
  3534.     {
  3535.         for (pos=0;pos<len;pos++)
  3536.         {
  3537.             if (isupper(string[pos]))
  3538.                 string[pos]=tolower(string[pos]);
  3539.         }
  3540.     }
  3541.  
  3542.     pos=current_x+1;
  3543.  
  3544.     for (line=current_y+(current_page-1)*LINES_PER_PAGE;line<numlines;line++)
  3545.     {
  3546.         cont=0;
  3547.  
  3548.         for (;pos<LINE_LENGTH;pos++)
  3549.         {
  3550.             if (case_type==CASE)
  3551.             {
  3552.                 if (string[cont]==file_buffer[line*LINE_LENGTH+pos])
  3553.                 {
  3554.                     if (cont==0)
  3555.                         goodpos=pos;
  3556.     
  3557.                     cont++;
  3558.                 }
  3559.                 else
  3560.                     cont=0;
  3561.  
  3562.                 if (cont>=len)
  3563.                     break;
  3564.             }
  3565.             else
  3566.             {
  3567.                 char tmpch;
  3568.  
  3569.                 if (isupper(file_buffer[line*LINE_LENGTH+pos]))
  3570.                     tmpch=tolower(file_buffer[line*LINE_LENGTH+pos]);
  3571.                 else
  3572.                     tmpch=file_buffer[line*LINE_LENGTH+pos];
  3573.  
  3574.                 if (string[cont]==tmpch)
  3575.                 {
  3576.                     if (cont==0)
  3577.                         goodpos=pos;
  3578.     
  3579.                     cont++;
  3580.                 }
  3581.                 else
  3582.                     cont=0;
  3583.  
  3584.                 if (cont>=len)
  3585.                     break;
  3586.             }
  3587.         }
  3588.  
  3589.         pos=0;
  3590.  
  3591.         if (cont>=len)
  3592.             break;
  3593.     }
  3594.  
  3595.     if (cont>=len)
  3596.     {
  3597.         current_x=goodpos;
  3598.  
  3599.         current_y=line%LINES_PER_PAGE;
  3600.  
  3601.         current_page=line/LINES_PER_PAGE+1;
  3602.  
  3603.         return 1;
  3604.     }
  3605.  
  3606.     for (line=0;line<current_y+(current_page-1)*LINES_PER_PAGE;line++)
  3607.     {
  3608.         cont=0;
  3609.  
  3610.         for (pos=0;pos<LINE_LENGTH;pos++)
  3611.         {
  3612.             if (case_type==CASE)
  3613.             {
  3614.                 if (string[cont]==file_buffer[line*LINE_LENGTH+pos])
  3615.                 {
  3616.                     if (cont==0)
  3617.                         goodpos=pos;
  3618.     
  3619.                     cont++;
  3620.                 }
  3621.                 else
  3622.                     cont=0;
  3623.  
  3624.                 if (cont>=len)
  3625.                     break;
  3626.             }
  3627.             else
  3628.             {
  3629.                 char tmpch;
  3630.  
  3631.                 if (isupper(file_buffer[line*LINE_LENGTH+pos]))
  3632.                     tmpch=tolower(file_buffer[line*LINE_LENGTH+pos]);
  3633.                 else
  3634.                     tmpch=file_buffer[line*LINE_LENGTH+pos];
  3635.  
  3636.                 if (string[cont]==tmpch)
  3637.                 {
  3638.                     if (cont==0)
  3639.                         goodpos=pos;
  3640.     
  3641.                     cont++;
  3642.                 }
  3643.                 else
  3644.                     cont=0;
  3645.  
  3646.                 if (cont>=len)
  3647.                     break;
  3648.             }
  3649.         }
  3650.  
  3651.         if (cont>=len)
  3652.             break;
  3653.     }
  3654.  
  3655.     if (cont>=len)
  3656.     {
  3657.         current_x=goodpos;
  3658.  
  3659.         current_y=line%LINES_PER_PAGE;
  3660.  
  3661.         current_page=line/LINES_PER_PAGE+1;
  3662.  
  3663.         return 1;
  3664.     }
  3665.     else
  3666.     {
  3667.         if (first>0)
  3668.             error_message("Couldn't find pattern because:","It isn't present in this file.");
  3669.         else if (first==0)
  3670.             error_message("Couldn't find pattern because:","There aren't any more instances in this file.");
  3671.  
  3672.         return 0;
  3673.     }
  3674.  
  3675.     return 0;
  3676. }
  3677.  
  3678. int spell_menu()
  3679. {
  3680.     WINDOW *spell_win;
  3681.  
  3682.     int inputch=0,pos=0;
  3683.  
  3684.     static current_word=-1,last_x=-1,last_y=-1,last_page=-1;
  3685.  
  3686.     spell_win=newwin(4,21,1,49);
  3687.  
  3688.     mybox(spell_win,4,21);
  3689.  
  3690.     wattron(spell_win,A_REVERSE);
  3691.  
  3692.     mvwaddstr(spell_win,1,1,"Check Spelling");
  3693.  
  3694.     wattroff(spell_win,A_REVERSE);
  3695.  
  3696.     mvwaddstr(spell_win,2,1,"Next incorrect word");
  3697.  
  3698.     keypad(spell_win,TRUE);
  3699.  
  3700.     wmove(spell_win,1,1);
  3701.  
  3702.     wrefresh(spell_win);
  3703.  
  3704.     while(inputch!='\n'&&inputch!='\r'&&inputch!=27)
  3705.     {
  3706.         inputch=getch();
  3707.  
  3708.         switch(pos)
  3709.         {
  3710.             case 0:    mvwaddstr(spell_win,1,1,"Check Spelling");
  3711.  
  3712.                 break;
  3713.  
  3714.             case 1:    mvwaddstr(spell_win,2,1,"Next incorrect word");
  3715.  
  3716.                 break;
  3717.         }
  3718.  
  3719.         switch(inputch)
  3720.         {
  3721.             case 12:    wrefresh(curscr);
  3722.  
  3723.                     break;
  3724.  
  3725.             case KEY_DOWN:    pos++;
  3726.  
  3727.                     break;
  3728.  
  3729.             case KEY_UP:    pos--;
  3730.  
  3731.                     if(pos<0)
  3732.                         pos+=2;
  3733.  
  3734.                     break;
  3735.  
  3736.             case KEY_RIGHT:    werase(spell_win);
  3737.  
  3738.                     wrefresh(spell_win);
  3739.  
  3740.                     delwin(spell_win);
  3741.  
  3742.                     display_screen();
  3743.  
  3744.                     return -1;
  3745.  
  3746.             case KEY_LEFT:    werase(spell_win);
  3747.  
  3748.                     wrefresh(spell_win);
  3749.  
  3750.                     delwin(spell_win);
  3751.  
  3752.                     display_screen();
  3753.  
  3754.                     return -2;
  3755.         }
  3756.  
  3757.         pos=pos%2;
  3758.  
  3759.         wattron(spell_win,A_REVERSE);
  3760.  
  3761.         switch(pos)
  3762.         {
  3763.             case 0:    mvwaddstr(spell_win,1,1,"Check Spelling");
  3764.                 wmove(spell_win,1,1);
  3765.  
  3766.                 break;
  3767.  
  3768.             case 1:    mvwaddstr(spell_win,2,1,"Next incorrect word");
  3769.                 wmove(spell_win,2,1);
  3770.  
  3771.                 break;
  3772.         }
  3773.  
  3774.         wattroff(spell_win,A_REVERSE);
  3775.  
  3776.         wrefresh(spell_win);
  3777.     }
  3778.  
  3779.     werase(spell_win);
  3780.  
  3781.     wrefresh(spell_win);
  3782.  
  3783.     delwin(spell_win);
  3784.  
  3785.     display_screen();
  3786.  
  3787.     if (inputch==27)
  3788.         return 0;
  3789.  
  3790.     attron(A_REVERSE);
  3791.  
  3792.     mvaddstr(0,0,"                                                                        F1:Help ");
  3793.  
  3794.     attroff(A_REVERSE);
  3795.  
  3796.     refresh();
  3797.  
  3798.     switch(pos)
  3799.     {
  3800.         case 0:    get_spell_list();
  3801.  
  3802.             last_page=last_x=last_y=-1;
  3803.  
  3804.             if (num_words==-1)
  3805.                 return 0;
  3806.  
  3807.             if (num_words==0)
  3808.             {
  3809.                 error_message("No words to check because:","All words are spelled correctly.");
  3810.  
  3811.                 return 0;
  3812.             }
  3813.  
  3814.             current_word=0;
  3815.  
  3816.             while (do_search(word_list[current_word],CASE,-1)==0)
  3817.             {
  3818.                 current_word++;
  3819.  
  3820.                 if (current_word>=num_words)
  3821.                 {
  3822.                     error_message("No words to check because:","All words are spelled correctly.");
  3823.  
  3824.                     return 0;
  3825.                 }
  3826.             }
  3827.  
  3828.             display_screen();
  3829.  
  3830.             return 0;
  3831.  
  3832.         case 1:    if (num_words<0)
  3833.             {
  3834.                 error_message("Can't get next incorrect word because:","You haven't checked the spelling yet.");
  3835.  
  3836.                 return 0;
  3837.             }
  3838.  
  3839.             if (current_word>=num_words)
  3840.             {
  3841.                 error_message("Can't get next incorrect word because:","All words have been checked.");
  3842.  
  3843.                 return 0;
  3844.             }
  3845.  
  3846.             while (1)
  3847.             {
  3848.                 if (do_search(word_list[current_word],CASE,-1)==0)
  3849.                 {
  3850.                     current_word++;
  3851.  
  3852.                     last_page=last_x=last_y=-1;
  3853.  
  3854.                     if (current_word>=num_words)
  3855.                     {
  3856.                         error_message("Can't get next incorrect word because:","All words have been checked.");
  3857.     
  3858.                         return 0;
  3859.                     }
  3860.                 }
  3861.                 else
  3862.                 {
  3863.                     if (last_x==-1)
  3864.                     {
  3865.                         last_x=current_x;
  3866.         
  3867.                         last_y=current_y;
  3868.         
  3869.                         last_page=current_page;
  3870.  
  3871.                         break;
  3872.                     }
  3873.                     else if (last_x==current_x&&last_y==current_y&&last_page==current_page)
  3874.                     {
  3875.                         last_page=last_x=last_y=-1;
  3876.  
  3877.                         current_word++;
  3878.  
  3879.                         if (current_word>=num_words)
  3880.                         {
  3881.                             error_message("Can't get next incorrect word because:","All words have been checked.");
  3882.         
  3883.                                 return 0;
  3884.                         }
  3885.                     }
  3886.                     else
  3887.                         break;
  3888.                 }
  3889.             }
  3890.  
  3891.             display_screen();
  3892.  
  3893.             return 0;
  3894.     }
  3895. }
  3896.  
  3897. void get_spell_list()
  3898. {
  3899.     FILE *spell_fd;
  3900.  
  3901.     int line=0,page=1,counter,pos;
  3902.  
  3903.     char spell_file1[65],spell_file2[65],command[65],inputline[LINE_LENGTH+1];
  3904.  
  3905.     if (word_list!=NULL)
  3906.     {
  3907.         for (counter=0;counter<num_words;counter++)
  3908.             free(word_list[counter]);
  3909.  
  3910.         free(word_list);
  3911.  
  3912.         word_list=NULL;
  3913.  
  3914.         num_words=-1;
  3915.     }
  3916.  
  3917.     sprintf(spell_file1,"/tmp/hwpsp1.%d",getpid());
  3918.     sprintf(spell_file2,"/tmp/hwpsp2.%d",getpid());
  3919.  
  3920.     errno=0;
  3921.  
  3922.     spell_fd=fopen(spell_file1,"w");
  3923.  
  3924.     if (spell_fd==NULL)
  3925.     {
  3926.         error_message("Couldn't open temporary spell checking file because:",strerror(errno));
  3927.  
  3928.         return;
  3929.     }
  3930.  
  3931.     for (line=0;line<numlines;line++)
  3932.     {
  3933.         for (pos=0;pos<LINE_LENGTH;pos++)
  3934.         {
  3935.             if (file_buffer[pos+line*LINE_LENGTH])
  3936.                 fputc(file_buffer[pos+line*LINE_LENGTH],spell_fd);
  3937.             else
  3938.                 fputc(' ',spell_fd);
  3939.         }
  3940.  
  3941.         fputc('\n',spell_fd);
  3942.     }
  3943.  
  3944.     fflush(spell_fd);
  3945.  
  3946.     close(spell_fd);
  3947.  
  3948.     sprintf(command,"spell %s > %s",spell_file1,spell_file2);
  3949.  
  3950.     system(command);
  3951.  
  3952.     unlink(spell_file1);
  3953.  
  3954.     errno=0;
  3955.  
  3956.     spell_fd=fopen(spell_file2,"r");
  3957.  
  3958.     if (spell_fd==NULL)
  3959.     {
  3960.         error_message("Couldn't open temporary spell checking file because:",strerror(errno));
  3961.  
  3962.         return;
  3963.     }
  3964.  
  3965.     num_words=0;
  3966.  
  3967.     while (fgets(inputline,LINE_LENGTH+1,spell_fd))
  3968.         num_words++;
  3969.  
  3970.     rewind(spell_fd);
  3971.  
  3972.     word_list=(char **) malloc(num_words*sizeof(char *));
  3973.  
  3974.     if (file_buffer==NULL)
  3975.     {
  3976.         error_message("Couldn't spell check because:","Malloc error on word list.");
  3977.  
  3978.         num_words=-1;
  3979.  
  3980.         return;
  3981.     }
  3982.  
  3983.     for (line=0;line<num_words;line++)
  3984.     {
  3985.         char *tmpptr;
  3986.  
  3987.         fgets(inputline,LINE_LENGTH+1,spell_fd);
  3988.  
  3989.         tmpptr=strrchr(inputline,'\n');
  3990.  
  3991.         if (tmpptr)
  3992.             *tmpptr=0;
  3993.  
  3994.         tmpptr=strrchr(inputline,'\r');
  3995.  
  3996.         if (tmpptr)
  3997.             *tmpptr=0;
  3998.  
  3999.         word_list[line]=(char *)malloc(strlen(inputline)+1);
  4000.  
  4001.         if (word_list[line]==NULL)
  4002.         {
  4003.             error_message("Couldn't spell check because:","Malloc error on word in word list.");
  4004.     
  4005.             num_words=-1;
  4006.     
  4007.             return;
  4008.         }
  4009.  
  4010.         strcpy(word_list[line],inputline);
  4011.     }
  4012.  
  4013.     fclose(spell_fd);
  4014.  
  4015.     unlink(spell_file2);
  4016. }
  4017.  
  4018. void do_help()
  4019. {
  4020.     werase(textscr);
  4021.  
  4022.     mvwaddstr(textscr,0,4,"Help for hwp (Harry's Word Processor):");
  4023.  
  4024.     mvwaddstr(textscr,2,4,"hwp is Copyright 1993, Harry C. Pulley, IV.  hwp may be copied freely.");
  4025.  
  4026.     mvwaddstr(textscr,4,4,"To activate menubar, press F10.  To use a specific function press the");
  4027.     mvwaddstr(textscr,5,4,"appropriate function key.  Once in the menubar, use the arrow keys to");
  4028.     mvwaddstr(textscr,6,4,"navigate the menubar; use Enter, Up or Down to select a menu.  Once in");
  4029.     mvwaddstr(textscr,7,4,"a menu, use the Up and Down keys to move to the desired selection; use");
  4030.     mvwaddstr(textscr,8,4,"Enter key to select.  ESC cancels either the menubar or a menu.");
  4031.     mvwaddstr(textscr,9,4,"hwp automatically word wraps in both insert and overstrike mode.  Use");
  4032.     mvwaddstr(textscr,10,4,"the arrow keys, pageup/down and home/end to move around.  Use Delete and");
  4033.     mvwaddstr(textscr,11,4,"and Backspace to erase text.  To change the insert mode use the Insert");
  4034.     mvwaddstr(textscr,12,4,"key or choose Toggle Insertmode from the Options menu.  To change your");
  4035.     mvwaddstr(textscr,13,4,"header or footer, use the Edit menu.  To load, save, print, new a");
  4036.     mvwaddstr(textscr,14,4,"file or quit use the File menu; to save a file as ASCII use an ending");
  4037.     mvwaddstr(textscr,15,4,"other than .hwp.  To change margins, tab size, etc. use the Options");
  4038.     mvwaddstr(textscr,16,4,"menu.  To find words in the text, use the find menu.  To check the");
  4039.     mvwaddstr(textscr,17,4,"spelling of your document, use the Spell menu; choose Next Incorrect");
  4040.     mvwaddstr(textscr,18,4,"Word to go to the next misspelled word.  CTRL-L refreshes the screen.");
  4041.  
  4042.     mvwaddstr(textscr,20,4,"Press any key to go on...");
  4043.  
  4044.     wrefresh(textscr);
  4045.  
  4046.     while (getch()==12)
  4047.         wrefresh(curscr);
  4048.  
  4049.     display_screen();
  4050. }
  4051.